perl - Error code 302 from HTTP POST operation -
I have a Perl script which posts data to a web service that I wrote in PHP ...
This code is:
used LWP :: UserAgent; My $ ua = LWP: UserAgeent-> New; My $ server_endpoint = "http://example.com/"; My $ req = HTTP :: Request-> New (post = & gt; $ server_endpoint); $ Req-> Header ('content-type' = & gt; 'app / jason'); $ Req-> Header ('x-auth-token' = & gt; 'kfksj48sdfj4jd9d'); # HTTP request body my Post_data $ = '{ "" Name ":" Dan "," address ":" NY "} Add post data'; $ Req- & gt; materials ($ post_data); my $ resp = $ Ua- & gt; Request ($ req); if ($ resp-> is_success) {my $ message = $ resp-> decode_ content; print "received answer: $ message \ n";} other { Print "HTTP POST error code:", $ resp-> code, "\ n"; print "HTTP POST error message:", $ resp-> message, "\ n";} < / Pre> When I send a request, I get this response:
HTTP POST error code: 302 HTTP POST error Message: Question:
- How can I get rid of this error or is it an error, even though it says?
- How can I get the return value of the post?
- What is the correct way to post the data? (The above code is my php site receives the post data and resounds or It prints as a back.)
Thanks in advance.
There is redirection instructions for a 302 error client from a server. If you do have LWP :: So UserAgent default configuration, it will follow automatically seven times the maximum redirection if you do not get a successful response, suggesting that either you redirects are closed (which is not likely the code you posted, unless you LWP :: UserAgent ), or you're stuck in a redirect loop. You can check the redirection data by checking the HTTP :: response object: my $ resp = $ ua-> gt; ; Request ($ req); # Check the success, etc ... if ($ resp-> is_redirect) {# Check the number of redirects made by the script: "n redirects:" $ Resp-> Redirects; } With the default LWP :: UA settings, there are seven numbers of the maximum redirections you received before the LWP :: UA.
Redirects $ resp-> Redirect is available by calling in the context of the array: # @redirects HTTP :: is an array of response objects, my @redirects = $ resp-> Redirects; # To print the redirection of each Response object, print the 'Location' header: Say "Location:". $ _- & gt; Headers ('Locations') for @redirects; # Or, for more comprehensive troubleshooting, print the full response: "Response:" $ _- & gt; For as_string @redirects; The example produced for the request of google.com, which once redirects:
# say "n redirects:". $ Resp-> Redirects; N redirects: 1 # say "location:" $ _- & gt; Headers ('Locations') for @redirects; Location: http://www.google.com. answer. $ _- & gt; For as_string @redirects; Feedback: HTTP / 1.1 302 Miller Cache-Control: Private Connection: Closed Date: Fri, 10 October 2014 10:45:41 GMT Location: http://www.google.co.uk/?gfe_rd=cr&ii=1bg3VJikJ_HH8gfOk4GwDw Server: GFE / 2.0 Content-Length: 261 Content-Type: Text / html; Charset = UTF-8 Optional-Protocol: 80: quic, p = 0.01 Client-date: Fri, 10 October 2014 10:45:39 GMT Client-peer: 74.125.230.102:80 Client-answer-number: 1 Title: 302 Moved & lt; HTML & gt; & Lt; HEAD & gt; & Lt; Meta http-equiv = "content-type" content = "text / html; charset = UTF-8" & gt; & Lt; TITLE & gt; 302 moved & lt; / TITLE & gt; & Lt; / HEAD> & Lt; Body & gt; & Lt; H1 & gt; 302 moved & lt; / H1> Document & lt; A HREF = "http://www.google.co.uk/?gfe_rd=cr&i.ei=1bg3VJikJ_HH8gfOk4GwDw" & gt; Here & lt; / A & gt; has been moved. & Lt; / Body & gt; & Lt; / Html & gt; My guess is that you are stuck in a redirect loop, and that is why you are not getting the expected response from your PHP scripts.
NB: To enable adds and other useful features, put it in Perl 5.10 and later, Usage facility ': 5.10' ;
Comments
Post a Comment