Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I read the response to a HTTP POST

I need to send a HTTP POST request to a web server and read the response that the server sends back. I know how to use the POST specification and I am able to read the response using the TCP read functions, but here's my stumbling block: The response data has a header segment that specifies the number of bytes in the content. If I can read the HTTP header, then I know exactly how many bytes to read in the content (instead of waiting till the timeout period is over).

For example, here's the HTTP response to a POST:


HTTP/1.1 200 OK
Date: Thu, 03 Jan 2008 20:06:30 GMT
Server: Mongrel 1.0.1
Status: 200 OK
Cache-Control: no-cache
Content-Type: application/xml; charset=utf-8
Content-Length: 389

<?xml version="1.0" encoding="UTF-8"?>
<test-run>
  <some data here>
</test-run>

Currently, if I can read the header, then I can parse out Content-Length and then read that many bytes in the rest of the body before stopping. What's the best way to do this?

The current method I'm using is attached.

0 Kudos
Message 1 of 5
(4,778 Views)
This should really be in the LabVIEW forum.

Also, you did not include the subVI that actually does the parsing.
0 Kudos
Message 2 of 5
(4,764 Views)

Hi Anil,

Another option would be to place the TCP Read VI inside a while loop and read until an error occurs.  By connecting the error out of the TCP Read to the conditional terminal of the while loop, the loop will stop on an error.  Example HTTP Protocols on DeveloperZone has code that demonstrates this. 

Jennifer R.
National Instruments
Applications Engineer
0 Kudos
Message 3 of 5
(4,751 Views)

Not sure if that last link to http helped you. It sure helped me, i will have to remember error 66.

However, i'm working on some UPNP and Soap calls to devices on the network and i wrote some send recieve vi's. What i noticed was all my devices return 8 lines which i consider a Header. It includes the HTTP OK, some device info and Content-Length:  What i do is send my TCP Soap call then read in a for loop, 8 times. Using CRLF as the end byte for each loop. then as i recieve each line i check for Content-Length: and if its there i pull out the number and save it in a shift register. then after the loop ends i convert string number to dec number and then do one more read with that number for the bytes to read. I'm not getting big files sent back to me so i think im okay. not sure what would happen if i tried to read a big file that way. Probably crash.

So my other thought was that if you only get like 3 lines in the header then my vi wouldn't work. so maybe you read until you recieve an empty string, which is the standard. To send a blank line at the end of each response. Then after that the data is read. Based of the parsed Content-Length:

 

Hope this helps someone. Maybe i can put my vi's together and post what i have if anyone is intrested.

- A minute saved is a Minute earned!
0 Kudos
Message 4 of 5
(4,513 Views)

Hi pRoFiT,

see this link please: http://forums.ni.com/ni/board/message?board.id=170&view=by_date_ascending&message.id=324049#M324049. The example shows in the "get" case how you can read the response.

Hope it helps.
Mike

0 Kudos
Message 5 of 5
(4,504 Views)