Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Perl CGI return 400 Bad request response with JSON content

by dineshyagappan (Initiate)
on Apr 02, 2022 at 15:56 UTC ( [id://11142635]=perlquestion: print w/replies, xml ) Need Help??

dineshyagappan has asked for the wisdom of the Perl Monks concerning the following question:

Able to return the JSON content using Perl CGI header with status as 200. But when I try to return JSON content using Perl CGI header with status "400 Bad Request". Its not coming as application/json content instead its coming as html. Could someone help on this?

Replies are listed 'Best First'.
Re: Perl CGI return 400 Bad request response with JSON content
by Anonymous Monk on Apr 02, 2022 at 16:20 UTC
    Could someone help on this?

    no, because you didnt show any code. read SSCCE

      working as expected. Return status as 200 and content in JSON
      print CGI::header({type => 'application/json', status => '200 OK', print "$json_encoded_data";
      Not working code, Returning as 400 status but content type is html only.
      print CGI::header({type => 'application/json', status => '400 Bad Requ +est', print "$json_encoded_data";

        Let's look at the RFC for HTTP status code 400:

        6.5.1. 400 Bad Request The 400 (Bad Request) status code indicates that the server cannot +or will not process the request due to something that is perceived to +be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

        Basically, "400" code tells the server that you did not properly understood the request or refused to parse it. Depending on your webserver software, it may choose to interpret that to mean "i could not make sense of this, send the default error page".

        Are you SURE this is actually the error code you want to use? It basically means "i did not understand your request at all".

        Unless the client sent complete garbage, in all likelyhood there is a HTTP status code from the 4xx group that will fit better. Take a look.

        perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

        Your code has syntax errors and does not compile. Please help us help you by making your problem reproducible.

        What server are you using to host this service? How is it set up? Which exact response (including all headers) are you receiving in both cases?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11142635]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found