Page 1 of 2
API JSON return "No header received"
Posted: Thu Mar 26, 2015 5:09 pm
by SureTint
I'm trying to work with a restful API that returns JSON, but I'm getting the error referenced in the subject. The code i have is:
Code: Select all
put "APIURL?pin=" && the text of field txtPIN into tAPIReq
replace " " with "" in tAPIReq
put URL tAPIReq into tJsonResp
put the result in theError
In this case the text of txtPIN is "1". tJsonResp is always empty, but theError is always nice and populated.
This call works fine when used through POSTMan or even directly in a browser and is even currently integrated into both a Windows application and iOS App, but is giving me issues with LiveCode. I'm assuming I need to set the http headers, but haven't found a good explanation of setting those on returned information, only on sent information.
Any help would be appreciated!
Re: API JSON return "No header received"
Posted: Thu Mar 26, 2015 6:41 pm
by mwieder
Russ-
Not a lot of information to go on there, but it looks like you're trying to do a GET command to retrieve JSON data.
Did you specify a Content-Type of application/json in your headers? Do you need to specify a particular Accept header?
And you don't fiddle with received headers, they come back from the server. My guess is that the "no header received" message is the server saying that your sent headers are incorrect.
Re: API JSON return "No header received"
Posted: Thu Mar 26, 2015 7:21 pm
by SureTint
Thanks for the reply mweider.
I realized after reading your response, I should have listed what gets returned from the call:
PIN invalid:
PIN valid:
Code: Select all
{"GlobalId":"E2411D32BFAB4A578E90C65056EB19AC","UserName":"Britany","PIN":"1","Location":"73326443A14B47BCA37115CB75CEC375"}
Returned headers (in both cases):
Cache-Control = private
Content-Length = [length of the content]
Content-Type = application/octet-stream
Date = [Correct date/time]
There are definitely headers available, but LiveCode doesn't seem to like them. Am I using the PUT command incorrectly considering the Content-Type header?
Re: API JSON return "No header received"
Posted: Thu Mar 26, 2015 10:20 pm
by mwieder
Russ-
The Content-Type headers don't necessarily have to match up. In one case you're telling the server what kind of data you're sending it, and for the return case the server is telling you what it's giving back.
I don't see anything wrong with you PUT statement, although I normally put parentheses around the operand for readability:
Would help to know how you're setting the request headers. You should at least have
Code: Select all
set the httpheaders to "Content-Type: application/json"
and you can use the libURLLastHTTPHeaders() function to do a post-mortem on the headers. You might want to compare those with the headers you're supplying to Postman.
Re: API JSON return "No header received"
Posted: Thu Mar 26, 2015 10:52 pm
by bangkok
SureTint wrote:Code: Select all
put "APIURL?pin=" && the text of field txtPIN into tAPIReq
replace " " with "" in tAPIReq
put URL tAPIReq into tJsonResp
put the result in theError
Are you sure about your URL and the way you create your query ? Because "APIURL" doesn't look like... an URL.
Better safe than sorry. To verify, you should add at the end :
Regarding the headers, I usually set mine (for JSON queries) like :
Code: Select all
Accept: application/json, text/javascript
Content-Type: application/json; charset=UTF-8
With :
Last but not least, you can use the excellent "Fiddler", a proxy to investigate the exchanges between server and client. You can use it with Livecode by using the command :
Code: Select all
set the HTTPProxy to "127.0.0.1:8888
http://www.telerik.com/fiddler
Re: API JSON return "No header received"
Posted: Thu Mar 26, 2015 11:55 pm
by mwieder
The AIPURL thing caught my eye too, but since he's getting a response from the server I figured it was just pseudocode so as not to have to publish the actual url. And also the "&&" instead of "&", although the next line removes the extra space anyway. But agreed, better safe than sorry. Good to ask the base-level questions first.
Re: API JSON return "No header received"
Posted: Fri Mar 27, 2015 3:01 am
by SureTint
Thanks to all responders.
mweider is correct, the "APIUrl" is just to hide the actual URL of the web service.
I have set the http headers to your suggestion bangkok, but to no avail. Still not getting a response string into the variable, just the "no headers" error referenced initially in this thread.
The full error I get is "error Redirect failed APIUrl/checksecurity/?pin=1 - error No header received"
Any ideas of a different approach to getting the results of the API call? Perhaps there's another direction I should be pursuing within LiveCode.
Thanks again.
Re: API JSON return "No header received"
Posted: Fri Mar 27, 2015 8:31 am
by SparkOut
Are you setting authentication cookies in the headers? Browsers do this behind the scenes but if you are creating the actions in LiveCode you have to manage the sessions yourself.
This thread may be helpful
http://forums.livecode.com/viewtopic.ph ... ers#p86063
Re: API JSON return "No header received"
Posted: Fri Mar 27, 2015 12:20 pm
by SureTint
Thank you for the reply Sparkout.
This site does not use authentication as it simply a testing site used for RAD proof of concept. I will definitely remember to look at that for production however.
Re: API JSON return "No header received"
Posted: Fri Mar 27, 2015 3:43 pm
by bangkok
Since i'm currently working on a project with JSON, i probably went too fast regarding the headers.
-you don't send a JSON string
-you just do a regular "put URL"
You could :
-with your browser, connect to the URL
-copy the header that is being sent
-then with Livecode, set the httpheader with a copy of it, to emulate your browser
Other point :
-you speak about a header sent and received : "
Content-Type = application/octet-stream"
That's weird because it's for binary transfert ?
Last but not least :
-the last error you received is weird too : "error
Redirect failed APIUrl/checksecurity/?pin=1 - error No header received"
What about the script on the server side ? Do you have control over it ? Is the script doing a
redirection ???
Re: API JSON return "No header received"
Posted: Fri Mar 27, 2015 4:33 pm
by SureTint
No problem Bangkok
You are correct, the return is a binary stream "octet-stream". Is this an issue in LiveCode?
I do have Web, Windows Forms and iOS Apps working with the API currently so I didn't really see it as an issue.
Thanks again to all responders.
Re: API JSON return "No header received"
Posted: Fri Mar 27, 2015 5:06 pm
by SureTint
I went ahead and tried to use the binary method for getting the data, but still no luck.
Here is the code I tried:
Code: Select all
on mouseUp
if the text of field txtPIN is empty then
answer "I couldn't see a PIN number for you. Give it another try."
else
put "APIURL/checksecurity?pin=" & the text of field txtPIN into tAPIReq
put URL("binfile:" & tAPIReq) into tJsonBinary
read from file tJsonBinary until end
put it into tJsonResp
close file tJsonBinary
answer tJsonResp
put the result into theError
if theError is empty then
put jsonToArray(tJsonResp) into arrLoginResp
if arrLoginResp["Response"] is "Not Found" then
answer "I don't recognize that PIN. Can you try again?"
else
answer "Found You!"
end if
else
answer "There was an error. Please try again."
end if
end if
end mouseUp
Re: API JSON return "No header received"
Posted: Fri Mar 27, 2015 5:44 pm
by mwieder
Russ-
Code: Select all
put URL("binfile:" & tAPIReq) into tJsonBinary
is entirely wrong. I don't think your original line was at fault.
But "error Redirect failed" is new information here. And that's a weird message when combined with the header message.
It looks like the server expects to do a page redirect after authentication, but it's not clear to me what's not happening.
I'd investigate a working flow with WireShark to see what's going on, and then look at the failing instance to see what's different. Browsers will happily follow redirections without clueing you in to what's going on under the hood, while you may have to code this explicitly if you're working at a lower level.
Re: API JSON return "No header received"
Posted: Fri Mar 27, 2015 6:29 pm
by SureTint
Thanks for the reply.
I'm trying to get a reply posted, but am having issues with the BB accepting the post. I have a note into the Forum administrator currently.
Re: API JSON return "No header received"
Posted: Fri Mar 27, 2015 7:02 pm
by SureTint
Figured I'd go ahead and post my reply without the included code as some setting on the forum is preventing that post.
I have multiple lower-level code sets accessing the API currently in ASP(dot)net, Swift and c# with no issue. For example, the API is used in c# and ASP(dot)net using a standard HTTPRequest object. In all other cases, the reply comes back to the application just fine and I'm able to parse it and use the response.
Can anyone confirm if LiveCode is just simply not set up to work with this type of binary reply? I don't want to bang my head against the wall if it's a lost cause.
FYI, figured out is was the unencoded "ASP(dot)net" that was preventing my post.