0

I am trying to communicate with anREST API using cURL and PHP. But, I am getting an issue from the API and I need to be able to see my HTTP request in plain text where I can analyses it and correct it.

I tried the following to write my request to a file

        $file = fopen('request.txt', 'w');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        curl_setopt($ch, CURLOPT_VERBOSE, true);    
        curl_setopt($ch, CURLOPT_STDERR, $file);        

But this does not show me the JSON data.

Here is what I get in the request.txt file

* Hostname servername was found in DNS cache
*   Trying INTERNAL IP...
* Connected to servername (INTERNAL IP) port 8018 (#0)
> PUT /icws/1163386002/messaging/subscriptions/queues/blahblah HTTP/1.1
Host: servername:8018
Accept: */*
ININ-ICWS-CSRF-Token: WAhtYWxoYXlla1dBY2GHSDMNDFtjMDcwZmIyYy1mguc4LTQ3YjEtODgzMy1iOTBkM2ZhYWHfoyNmYTlYCjEwLjAuNC4xNjA=
ININ-ICWS-Session-ID: 1163386002
Cookie: icws_1163386002=232sggsdfgdabe-404c-8d8c-12345dfgdfg
Content-Length: 156
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 156 out of 156 bytes
< HTTP/1.1 500 Internal Server Error
< Cache-Control: no-cache, no-store, must-revalidate
< Pragma: no-cache
< Expires: 0
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
< Content-Type: application/vnd.inin.icws+JSON; charset=utf-8
< Date: Tue, 12 May 2015 17:52:52 GMT
< Server: HttpPluginHost
< Content-Length: 90
< 
* Connection #0 to host servername left intact

I would like to see the JSON string in plain text.

Here is how I am doing my cURL call using PHP

private function _processRequest($method, $uri, $data = false, $header = NULL, &$httpRespond = array(), &$httpCode = NULL)
{
    $ch = curl_init();
    $url = $this->_baseURL . $uri;

    if( 
           ($method == 'POST' || $method == 'PUT') 
        && $data
    ){
        $jsonString = json_encode( $data );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $jsonString );
    }

    if($method == 'POST'){
        curl_setopt($ch, CURLOPT_POST, true);
    } elseif( $method == 'PUT'){
        //curl_setopt($ch, CURLOPT_PUT, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    } else {
        if ($data){
            $url = sprintf("%s?%s", $url, http_build_query($data));
        }
    }   

    //disable the use of cached connection
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

    //return the respond from the API
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //return the HEADER respond from the API
    curl_setopt($ch, CURLOPT_HEADER, true);

    //add custom headers
    if(!empty($header)){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }

    //enable SSL
    if( $this->_protocol == 'https' ){
        curl_setopt($ch, CURLOPT_CAINFO, $this->_cainfo);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
    }

    //set the URL
    curl_setopt($ch, CURLOPT_URL, $url);

        $file = fopen('request.txt', 'w');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        curl_setopt($ch, CURLOPT_VERBOSE, true);    
        curl_setopt($ch, CURLOPT_STDERR, $file);        


    $respond = curl_exec($ch);

    //throw cURL exception
    if($respond === false){
        $errorNo = curl_errno($ch);
        $errorMessage = curl_error($ch);

        throw new ApiException($errorMessage, $errorNo);
    }   

    list($header, $body) = explode("\r\n\r\n", $respond, 2);

    $httpRespond = $this->_http_parse_headers($header);


    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  

    $result = json_decode($body, true);

    return $result;
}

How do I see the entire HTTP request in a plain text?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Jaylen
  • 39,043
  • 40
  • 128
  • 221
  • You get an HTTP 500 error response. Are you sure your application's providing a body with JSON in case of internal error? – marekful May 12 '15 at 18:07
  • Yes I get an HTTP 500 internal error. This is the respond from the API, When I look at the API logs it I get this "This jsonlib::Value is not of type eValueType_Array" so I would like to see the plain text to see if the json has the correct data – Jaylen May 12 '15 at 18:10

1 Answers1

0

You can use Fiddler (http://www.telerik.com/fiddler) to see your complete request/response in plain text and to test your REST API as well.

Also, you can use Wireshark (https://www.wireshark.org/) applying a HTTP filter.

My recommendation is Fiddler, I use it a lot to test and debug my REST API.

Hope this helps

CrApHeR
  • 2,595
  • 4
  • 25
  • 40
  • How do I user Fiddler? do I install it on the apache server, my PC, or the API server? in what section i can see my request in plain text? note that my Apache server is HTTPS but I am sending the request at HTTP only – Jaylen May 12 '15 at 18:19
  • You have to install it in the client computer where you are doing your request. – CrApHeR May 12 '15 at 18:24
  • okay, that is what I have done. I do see the request on the left but where would I find the request in plain text? – Jaylen May 12 '15 at 18:25
  • Double click on the request. at the right of the screen you will have the screen split in 2 (top and bottom). Top is your complete request, bottom is your complete response. In the tabs (Top and Bottom) you have the option Raw. There you have your raw request (top) and raw response (bottom) – CrApHeR May 12 '15 at 18:27
  • I am not sure what I am missing. I clicked on the request from the left pan. Then on the right "top" the clicked on the "textView" tab. but it seems that the results are encrypted. and in the JSON tab I only see 2 nodesx (ie. `0` `x17`) – Jaylen May 12 '15 at 18:31
  • Here you have an example: http://imgur.com/S2RFCNa . Double click on green and you will see the right screen (Red is the request, light blue is the response). Click on Raw (Purple), that is the Raw Request sent from the client. Click on Raw (Yellow), that is the Raw Response received from the server. – CrApHeR May 12 '15 at 18:33
  • Thank you... when I click on the "Raw" purple or yellow I see the data but it is encrypted. here is an example of one of the lines `SessionID: BF DD 91 CC C7 7D DB F0 56 9F BB C1 B2 68 AB C3 ED E7 03 44 48 CE EC F0 50 CD 29 55 1A 15 42 56` how would I decrypt the data? – Jaylen May 12 '15 at 18:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77647/discussion-between-crapher-and-mike). – CrApHeR May 12 '15 at 18:41