0

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>
5
  • @Jesse_b Are you sure about that edit? Feb 27, 2018 at 13:26
  • @GerardH.Pille: No but it's better than it was.
    – jesse_b
    Feb 27, 2018 at 13:26
  • Cannot decide wether this question fits here or is stackoverflow.com material. Feb 27, 2018 at 13:27
  • @RuiFRibeiro Move on then, there's nothing here but us chickens. Feb 27, 2018 at 13:30
  • yes i m sure. @Jesse_b
    – Tiger
    Feb 27, 2018 at 13:34

2 Answers 2

2

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

0

"session" contains a carriage return, which ruins your curl request. Replace the grep/head/cut in backticks by:

session=$(awk '/^X-RestSvcSessionId/ {print $2}' session.xml)

and use the following curl parameter:

--cookie "X-RestSvcSessionId=$session"
7
  • not working :-(
    – Tiger
    Feb 27, 2018 at 13:48
  • Can you publish your session.xml somewhere? Feb 27, 2018 at 14:01
  • 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-qrstuvw Server: Microsoft-HTTPAPI/2.0 X-RestSvcSessionId: NWJlYjVlZWItMWFmOS00YjU0LWJkOWYtNDA5MTA4NjEwODhi Set-Cookie: X-RestSvcSessionId=NWJlYjVlZWItMWFmOS00YjU0LWJkOWYtNDA5MTA4NjEwODhi; Path=/api; Date: Tue, 27 Feb 2018 13:47:46 GMT
    – Tiger
    Feb 27, 2018 at 14:05
  • 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. Feb 27, 2018 at 14:08
  • My mistake, bash starts a subshell for the pipe, so the variable session is lost after the read. Feb 27, 2018 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.