0

I need to send a json post request to a url. I am using php curl class. But my request header is always showing content type as application/x-www-form-urlencoded if even I have set up content type. Here is my code

 $curl1 = new Curl();

$curl1->setOpt(CURLOPT_RETURNTRANSFER, TRUE);
$curl1->setOpt(CURLOPT_HEADER, FALSE);
$curl1->setOpt(CURLOPT_POST, TRUE);
$curl1->setOpt(CURLOPT_POSTFIELDS, $data);

$curl1->setOpt(CURLOPT_HTTPHEADER, array(
    "content-type:application/json"

));
$curl1->setBasicAuthentication('username', 'password');

$curl1->post('https://example.com/url');

I am getting 500 error from api. I have contacted them, they said your content type isn't right. I have print out my content type that looks like

CaseInsensitiveArray Object
(
[container:Curl\CaseInsensitiveArray:private] => Array
    (
        [Request-Line] => POST /url HTTP/1.1
        [Authorization] => Basic Yccx4MzA1ZTU0MDkwNDA3OGI1OWI4YjUyNjI4MjJjNTM6MTddfhf7755Y1ZDQ0NDVmY2I2NTEzZjllNTU3MzI4MTM=
        [User-Agent] => PHP-Curl-Class/3.4.5 (+https://github.com/php-curl-class/php-curl-class) PHP/5.5.24 curl/7.36.0
        [Host] => ssapi.shipstation.com
        [Accept] => */*
        [Content-Type] => application/x-www-form-urlencoded
    )

)

Please check the content type in request headers and in my code. They are different. I am setting Content-Type: applicaiton/json then why in request it is showing application/x-www-form-urlencoded?

And here is the json that I am sending

 {"orderNumber":"BENORDER","orderDate":"2015-05-31T03:38:36-0700","orderStatus":"awaiting_shipment","billTo":{"name":"Stack Yu","company":null,"street1":" Stockton AVenue","city":"Plainfield","state":"IL","postalCode":"44444"},"shipTo":{"name":"Stack Yu","company":null,"street1":" Stockton AVenue","city":"Plainfield","state":"IL","postalCode":"44444"},"items":[{"sku":"S102","name":"My Prodcut","quantity":"1"}]}

Any help will be much appreciated!

1 Answer 1

1

php-curl-class has a separate method for setting a header, check the documentation. So, you have to use:

$curl1->setHeader("Content-Type", "application/json");

and the Content-Type header will be set.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.