Help with PUT request error

(10 posts) (2 voices)

Tags:

No tags yet.

  1. Rbert, Member

    Hello,

    I'm using this code to try and send a PUT request:

      select 'dbr.http.debug', 1;
    select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', concat('authorization: ', @token);
    select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'accept: application/json';
    select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'content-type: application/json'; select 'dbr.http.option', 'curl', 'CURLOPT_POSTFIELDS', json_object('orgId', @org_id); select 'dbr.http.put', @session_url;

    But this is the debug output I'm receiving:
    * Trying hidden... * TCP_NODELAY set * Connected to hidden port 443 (#0) * ALPN, offering h2 * ALPN, offering http/1.1 * successfully set certificate verify locations: * CAfile: /etc/ssl/certs/ca-certificates.crt CApath: /etc/ssl/certs * SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256 * ALPN, server accepted to use h2 * Server certificate: * subject: CN=getsling.com * start date: Feb 21 00:00:00 2023 GMT * expire date: Jan 2 23:59:59 2024 GMT * subjectAltName: host hidden * issuer: C=US; O=Amazon; CN=Amazon RSA 2048 M02 * SSL certificate verify ok. * Using HTTP2, server supports multi-use * Connection state changed (HTTP/2 confirmed) * Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0 * Failed sending POST request * Connection #0 to host hidden left intact Array ( [curl_getinfo] => Array ( [url] => hidden [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.010291 [namelookup_time] => 0.000598 [connect_time] => 0.001891 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 154 [starttransfer_time] => 0 [redirect_time] => 0 [redirect_url] => [primary_ip] => hidden [certinfo] => Array ( )

    [primary_port] => 443 [local_ip] => hidden [local_port] => 53868 [http_version] => 0 [protocol] => 2 [ssl_verifyresult] => 0 [scheme] => HTTPS [appconnect_time_us] => 10237 [connect_time_us] => 1891 [namelookup_time_us] => 598 [pretransfer_time_us] => 0 [redirect_time_us] => 0 [starttransfer_time_us] => 0 [total_time_us] => 10291 )

    [postfields] => [] [result] => )
    Specifically, I'm seeing "Failed sending POST request". Why is the server trying to send a POST request? Additionally, why is [postfields] empty?

    And do you see any issues with the MyDBR code?

    Thank you

  2. Rbert, Member

    If it helps, the python equivalent of what I'm trying to do is

    session_headers = { "accept": "application/json", "Content-Type": "application/json", "authorization": token } session_data = { "orgId": api_org_id } result = requests.put(session_api_url, headers=session_headers, json=session_data)
    using the `requests` library.

  3. myDBR Team, Key Master

    Instead of:

    select 'dbr.http.option', 'curl', 'CURLOPT_POSTFIELDS', json_object('orgId', @org_id);
    
    select 'dbr.http.put', @session_url;

    Can you try:

    select 'dbr.http.put', @session_url, @org_id as 'orgid';

    The latest build also includes info about the actual request being made (POST/PUT). The error message could be misleading.

    Also, can you try the same call from the command line curl?
    --
    myDBR Team

  4. Rbert, Member

    thanks for your answer

    I upgraded to the latest build, and changed my code to

      select 'dbr.http.debug', 1;
    select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', concat('Authorization: ', @token);
    select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'Accept: application/json';
    select 'dbr.http.option', 'curl', 'CURLOPT_HTTPHEADER', 'Content-Type: application/json'; select 'dbr.http.put', @session_url, @org_id as 'orgId';

    And the debug output I now get is
    * Trying hidden... * TCP_NODELAY set * Connected to hidden port 443 (#0) * ALPN, offering h2 * ALPN, offering http/1.1 * successfully set certificate verify locations: * CAfile: /etc/ssl/certs/ca-certificates.crt CApath: /etc/ssl/certs * SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256 * ALPN, server accepted to use h2 * Server certificate: * subject: CN=hidden * start date: Feb 21 00:00:00 2023 GMT * expire date: Jan 2 23:59:59 2024 GMT * subjectAltName: host "hidden" matched cert's "*.hidden" * issuer: C=US; O=Amazon; CN=Amazon RSA 2048 M02 * SSL certificate verify ok. * Using HTTP2, server supports multi-use * Connection state changed (HTTP/2 confirmed) * Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0 * Failed sending HTTP POST request * Connection #0 to host hidden left intact Array ( [REQUEST] => PUT [curl_getinfo] => Array ( [url] => hidden [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.010415 [namelookup_time] => 0.000597 [connect_time] => 0.001861 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 16 [starttransfer_time] => 0 [redirect_time] => 0 [redirect_url] => [primary_ip] => hidden [certinfo] => Array ( )

    [primary_port] => 443 [local_ip] => hidden [local_port] => 44686 [http_version] => 0 [protocol] => 2 [ssl_verifyresult] => 0 [scheme] => HTTPS [appconnect_time_us] => 10375 [connect_time_us] => 1861 [namelookup_time_us] => 597 [pretransfer_time_us] => 0 [redirect_time_us] => 0 [starttransfer_time_us] => 0 [total_time_us] => 10415 )

    [postfields] => {"orgId":hidden} [result] => )

    So the request is still failing to be sent. Additionally, it seems like the content_type is empty, even though I'm setting it with curlopt.

  5. myDBR Team, Key Master

    The content_type is coming from the server you are connecting to. Now that the connection fails, it gets no value. myDBR does set the headers based on your commnds.

    Can you try the same request via command line curl with verbose settings.

    curl -v -X PUT https.//xxxx.xxx.xxx \ -H "Authorization: ABC" \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ -d "{orgId: 12}"

    --
    myDBR Team

  6. Rbert, Member

    This curl command works as expected, resulting in an 200 OK status, and a correct response. This is the curl command
    curl -v -X PUT hidden \ -H "authorization: hidden" \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ -d "{\"orgId\": hidden}"

  7. myDBR Team, Key Master

    OK,
    the next step is to try with PHP:

    <?php
    
    $url = 'https://xxx.xxx.xxx';
    $orgid = 1234;
    $auth = 'ABC'; echo '
    ';
    
    $out = fopen('php://output', 'w');
    $_debug_output = [];
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_STDERR, $out);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS,'{"orgId":'.$orgid.'}');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['authorization: '.$auth, 'accept: application/json', 'content-type: application/json']);
    $data = curl_exec( $ch );
    $info = curl_getinfo($ch);
    
    fclose($out);
    print_r($info);
    echo $data;
    echo '
    ';

    --
    myDBR Team

  8. Rbert, Member

    Again, works correctly with this php script.

  9. myDBR Team, Key Master

    Will transfer this to support for more detailed info.

    --
    myDBR Team

  10. Rbert, Member

    It was a stupid bug, not an issue on your side. The auth token had a trailing carriage return, failing the request.

    However, I would not have found this if not for the improved dbr.http.debug you added as a consequence of this thread, so thank you!


Reply

You must log in to post.