0

I have a shi**y API, which returns a broken JSON after GET request with HTTParty.

GET HTTParty.get('url/login/success/', headers: {cookie: "JSESSIONID=ID"}) returns JSON::ParserError (765: unexpected token at)

and response.body is

"{\"head\":{\"apikey\":null,\"sessionid\":\"ID\",\"timestamp\":1551772335837,\"sessiontimeout\":1551775035837,\"wishlistItemsCount\":0,\"basketItemsCount\":0,\"loggedIn\":true,\"role\":\"C\"},\"data\":{\"user\":{\"profile\":{\"title\":\"Title\",\"firstname\":\"Name\",\"lastname\":\"Lastname\",\"street\":\"Street\",\"street2\":\"number\",\"postalcode\":\"code\",\"city\":\"City\",\"customerID\":\"ID\",\"customerType\":xx}},\"abandonedBasket\":false},\"messages\":[{\"code\":\"url.api.login.success\",\"statusCode\":200,\"description\":\"OK\"}]}{\"head\":{\"apikey\":null,\"sessionid\":\"ID\",\"timestamp\":1551772335841,\"sessiontimeout\":1551775035841,\"wishlistItemsCount\":0,\"basketItemsCount\":0,\"loggedIn\":true,\"role\":\"C\"},\"data\":{},\"messages\":[{\"code\":\"url.api.general.error\",\"statusCode\":500,\"description\":\"Es ist ein interner Fehler aufgetreten. Bitte versuchen sie es später noch einmal.\"}]}"

Is there any possibility to fix the response's JSON for user to log in and use his account normally?

The alternative I have is using curl inside code, but I want to avoid it

Dende
  • 545
  • 6
  • 19
  • So you have no control over the API itself? If so then I would report the error to the person that does as this is clearly invalid `JSON`. Second how do you know which response is correct. Is it valid or an error? – engineersmnky Mar 05 '19 at 21:27
  • Right, no control over API. I assume that valid response is correct, since same request with curl returns valid response. – Dende Mar 06 '19 at 07:33

1 Answers1

0

Probably you are not sending JSON data.

Check this Changing Content-Type to JSON using HTTParty

  1. content_type
  2. to_json
Igor Kasyanchuk
  • 766
  • 6
  • 12
  • I do send JSON. The response's body somehow has two parts with malformed JSON – Dende Mar 05 '19 at 11:30
  • 1) do you specify content-type? 2) try to save response into json file and validate online it. 3) try to compare all headers (print debug info from httparty to console) and same with curl and check the difference – Igor Kasyanchuk Mar 05 '19 at 20:41