0

In short:

The Content-type I am setting while making a curl is being overwritten. Relevent code below:


Detailed

I am developing a mobile app backend application in codeigniter which needs to make a gcm call. For the same I am using the codeigniter-gcm library from, https://github.com/antongorodezkiy/codeigniter-gcm. And though it works well on my local machine it returns error when put on my server.

Upon inspection of the request I was making I found that the content-type that is set when making the curl is being overwritten. The content-type that is set is application/json but for some reason when making the request on the server, it is being over written to text/html as evidenced by outputting the request headers. This is the relevant code used.

    $headers[] = 'Content-Type:application/json';
    $headers[] = 'Authorization:key='.$this->apiKey;

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $this->apiSendAddress);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    $this->responseData = curl_exec($curl);
    $this->responseInfo = curl_getinfo($curl);

    curl_close($curl);

But notice the output when I output $this->responseInfo

[url] => https://android.googleapis.com/gcm/send
[content_type] => text/html; charset=UTF-8
[http_code] => 401
[header_size] => 395
[request_size] => 967
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.052485
[namelookup_time] => 2.7E-5
[connect_time] => 0.002536
[pretransfer_time] => 0.010841
[size_upload] => 786
[size_download] => 147
[speed_download] => 2800
[speed_upload] => 14975
[download_content_length] => -1
[upload_content_length] => 786
[starttransfer_time] => 0.05244
[redirect_time] => 0
[redirect_url] => 
[primary_ip] => 2404:6800:4005:80a::200a
[certinfo] => Array
    (
    )

Why does curl change the Content-Type? How can I make sure it is not changed? Any help would be appreciated. Thanks!

EDIT I tried the solution mentioned in PHP curl changes the Content-Type to application/x-www-form-urlencoded. Shouldn't do that but that too is being overwritten

Community
  • 1
  • 1
arunondeck
  • 368
  • 6
  • 16

1 Answers1

0

These are two different Content-Type headers. The first one you send in the request, the second one you get in the response. So it's normally that server responses with text/html. If you expect another content-type, use Accept header in your request.

umka
  • 1,655
  • 1
  • 12
  • 18
  • The server response is usually application/json. As per what I read it also supports text/plain. But even when I set Content-Type to that and also set the Accept header, curl is changing the send Content-Type to text/html. – arunondeck May 27 '15 at 10:46
  • How do you see request headers? Try [this way](http://stackoverflow.com/questions/866946/how-can-i-see-the-request-headers-made-by-curl-when-sending-a-request-to-the-ser) (the second answer) to write request headers to a file. – umka May 27 '15 at 13:22