how to put variable in CURL header using shell script?

10,262

The problem is if you SET variable by curl, grep, head or tail ++ then you will have a NEW Line in the END of your variable.

To remove the NEW Line set your last curl with $session like:

curl -H "X-RestSvcSessionId: ${session//[$'\t\r\n ']}" -X GET  "http://a.b.c.d:xxxx/api/jobs"

This will remove the new line and tab (if any).

I had same error: check here

Share:
10,262
Author by

Tiger

Updated on September 18, 2022

Comments

  • Tiger 3 months

    I want to call an API using shellscript but I m not able to put header properly...whats going wrong in my script... thanks in advance.

    shell script :

    #!/bin/bash
    curl -H "Authorization: Basic ABCDEFGHIJKLMNOPQRSTUVWXYZ" -H "Content-Type: application/xml" -X POST -i -d "" -o session.xml  http://a.b.c.d:xxxx/api/auth/
    session=`grep -i x-rest session.xml | head -1 | cut -d ' ' -f 2`
    echo $session
    set -xxx
    curl -H "X-RestSvcSessionId: ${session}" -X GET  "http://a.b.c.d:xxxx/api/jobs"
    

    output:

    <pre>
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  5475    0  5475    0     0  43457      0 --:--:-- --:--:-- --:--:-- 43800
    OGIyMDUxYTctNjMzNC00MjhjLWJiOTEtZjQ2YmMwZGY1NmY3
     -X GET http://a.b.c.d:xxxx/api/jobsjMzNC00MjhjLWJiOTEtZjQ2YmMwZGY1NmY3
    </pre>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
    <HTML><HEAD><TITLE>Bad Request</TITLE>
    <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
    <BODY><h2>Bad Request</h2>
    <hr><p>HTTP Error 400. The request is badly formed.</p>
    </BODY></HTML>
    
    • jesse_b
      jesse_b almost 5 years
      @GerardH.Pille: No but it's better than it was.
    • Rui F Ribeiro
      Rui F Ribeiro almost 5 years
      Cannot decide wether this question fits here or is stackoverflow.com material.
    • Gerard H. Pille
      Gerard H. Pille almost 5 years
      @RuiFRibeiro Move on then, there's nothing here but us chickens.
    • Tiger almost 5 years
      yes i m sure. @Jesse_b
  • Tiger almost 5 years
    not working :-(
  • Gerard H. Pille
    Gerard H. Pille almost 5 years
    Can you publish your session.xml somewhere?
  • Tiger almost 5 years
    HTTP/1.1 201 CREATED Transfer-Encoding: chunked Content-Type: application/xml; charset=utf-8 Location: http://W.X.Y.Z:xxxx/api/sessionMngr/abcd-efgh-ijkl-mnop-qrst‌​uvw Server: Microsoft-HTTPAPI/2.0 X-RestSvcSessionId: NWJlYjVlZWItMWFmOS00YjU0LWJkOWYtNDA5MTA4NjEwODhi Set-Cookie: X-RestSvcSessionId=NWJlYjVlZWItMWFmOS00YjU0LWJkOWYtNDA5MTA4N‌​jEwODhi; Path=/api; Date: Tue, 27 Feb 2018 13:47:46 GMT
  • Gerard H. Pille
    Gerard H. Pille almost 5 years
    Put it at least in your question, that some of the formatting remains. But this wiill still remove any special characters that you don't see.
  • Gerard H. Pille
    Gerard H. Pille almost 5 years
    My mistake, bash starts a subshell for the pipe, so the variable session is lost after the read.
  • Tiger almost 5 years
    still not working :-(
  • Gerard H. Pille
    Gerard H. Pille almost 5 years
    You need to send a cookie back, see my answer.