[solved] Trouble getting JSON response with QNetworkRequest



  • I successfully used QNetworkRequest to connect to a SOAP service and a website that returns its result in the html text.
    But when I try to connect to a service that returns JSON text I get an error (from them I'm assuming): "status" : error, "message" : "invalid format of ___ field". The same url http://foo/?user=abc&pass=123&field=foo works fine in Firefox.
    The only fields I'm setting in the header are: Accept = application/json text/* and Connection = Keep-Alive

    The code is just

    @
    QNetworkRequest req;
    req.setUrl(QUrl("http://foo/..."));
    req.setRawHeader("Accept", "...")
    QNetworkReply* reply = manager->post(request, "")
    @



  • Ummm.... You get JSON error as I understood, can you give more information? Raw response for example.



  • By "raw response" do you just mean the text I am getting back?
    It is: { “status” : error, “message” : “invalid format of ___ field” } (I've taken out the field name).

    When I enter the URL into Firefox I get a "Save as" dialog to save the json data and
    it works fine.



  • Ok, how do you set "Accept" header in firefox?
    Do you sure firefox not send any more data to server (cookie as example)?



  • Use wireshark to do a packet capture and compare your app with firefox to see what is different in the request.



  • I got it to work! I was reusing the code I wrote for the SOAP service so I had:
    @ manager->post(request, "") @

    I changed it to @ manager->get(request) @ and that did the trick.
    (Also, apparently the url parameters are case sensitive, though that wasn't the original issue.)

    EDIT: And of course, thanks to everyone!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.