question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Response body parsing - JSON::ParserError 757: unexpected token at '"some-string"'

See original GitHub issue

I am currently using PactNet 2.2.0 and PactNet.Windows 2.2.0 with a .NET 4.6.1 project. I have unit tests for my consumer service that generates a pact file (spec v1) against a provider service with an interaction like below. The interaction is for adding a new record and then returning its id in the response body. My provider’s service controller method simply returns a Guid and below is how the Guid response gets written to the pact file.

Pact interaction from consumer:

{
  "description": "AddWidget request returns 200 and the new widget Id",
  "providerState": "MyService does not have widgets",
  "request": {
    "method": "post",
    "path": "/widgets",
    "headers": {
      "Content-Type": "application/json; charset=utf-8"
    },
    "body": {
      "field1": "foo",
      "field2": "bar"
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json; charset=utf-8"
    },
    "body": "\"3e7d3384-26ff-401e-a256-b6eb23bc9402\""
  }
}

Provider service controller method:

[HttpPost]
[Route("widgets")]
public async Task<Guid> AddWidgetAsync([FromBody] Widget widget, CancellationToken cancellationToken)
{
     return await _widgetService.AddWidgetAsync(widget, cancellationToken).ConfigureAwait(false);
}

This all seems fine and unit tests work for the consumer. However, when I try to verify the pact file against the provider service I get the following error.

Failure/Error: let(:response_body) { parse_body_from_response(response) }

JSON::ParserError:
  757: unexpected token at '"3e7d3384-26ff-401e-a256-b6eb23bc9402"'

I’ve tried manually changing the interaction in the pact file to "body": "3e7d3384-26ff-401e-a256-b6eb23bc9402" (without the escaped double-quote), but I get the same above error.

I did some digging, and it appears that this error is coming from the Ruby JSON parser. I don’t know anything about Ruby, but from my internet searches it sounds like some versions of Ruby have a “quirks” mode to support parsing top level strings like this, but I couldn’t find any such mode in the Ruby distributed with PactNet.

It seems like if the pact file gets written out by PactNet, then PactNet should be able to read that same file back in, right? Are there any workarounds and/fixes to consider? Am I doing something wrong with my controller method? I’m basically looking for any help or recommendations. Thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Blackbaud-KrisMahoncommented, Feb 26, 2018

It is something we’ve done for a while with our endpoints, and it seems to work fine when a .NET service is talking to another .NET service. Pact is the first time we’ve hit an issue with the top level values (probably because .NET/Microsoft was letting us get away with it previously). I discussed the options with my team, and I think we are just going to change our endpoints to wrap the id in an object - that seems to be the least amount of effort for us right now given how everything else is setup. Thanks for the suggestions.

0reactions
bethesquecommented, Feb 26, 2018

👍 Cool, sounds like the best option.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ParserError) "757: unexpected token in rails
You're trying to parse a URL. JSON.parse(open("...").body) if you're using open-uri You need to read the response, you're not even sending a ...
Read more >
How to fix: "unexpected token" error for JSON.parse
The error above happens because the JSON you supplied is invalid. While to_json does work correctly, the result itself is not JSON that...
Read more >
[Solved]-JSON::ParserError 757 Unexpected token-ruby-on-rails
The problem for sure is the response, this is an invalid JSON, what you can do first of all move this code to...
Read more >
[Solved]-JSON::ParserError 757 Unexpected token-ruby
The problem for sure is the response, this is an invalid JSON, what you can do first of all move this code to...
Read more >
Json Parse error in Ruby: `parse': 795: unexpected token
I am getting this Json parse error in Ruby. I searched online and followed suggestions like gsub etc, but still the error persists....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found