Using mappedRequestHeaders in Spring XD - spring-xd

I am trying to simulate the following curl statement
curl -k -H "username: Your API Username" -H "password: Your API Password" https://infoconnect1.highwayinfo.govt.nz/ic/jbi/TrafficCameras/REST/FeedService/
How do I use mappedRequestHeaders to inject username and password into http-client stream?

I think what you're trying to do can be achieved by enriching the message flow with the two headers you want and then using mappedRequestHeaders to specify that those should be propagated.
Something like this:
whatever | modules | transform --script=enrich.groovy | http-client --mappedRequestHeaders=username,password
where enrich.groovy adds username and password as headers in the Spring Integration message.
This does call out for an easy way to pass http headers though, maybe as an option to the module. I created https://jira.spring.io/browse/XD-2372 to track this, if you're interested

Related

ecobee API thermostat request (json) using bash and curl

I'm writing a bash script to interface with my ecobee (query info and change settings). I have the authorization all worked out (access token and refresh token) and am now trying to request info from the ecobee. This json parameter list is dynamically created. None of the curl examples in the Developers API Doc seem to work.
I've tried assigning the json to a variable (?json="$selection") and to a file (?json=#"$location"). My latest attempt (hard coding the json and escaping the braces) of the curl is as follows:
response=$(curl -s "https://api.ecobee.com/1/thermostat?json=\{"selection":\{"selectionType":"registered","selectionMatch":null,"includeRuntime":true,"includeSettings":true/}/}" -H "Content-Type: application/json;charset=UTF-8" -H "Authorization: Bearer $access_token")
and I received a null response declare -- response=""
If I have curl read from a file:
{"selection":{"selectionType":"registered","selectionMatch":null,"includeRuntime":true,"includeSettings":true}}
then I receive:
response='{
"status": {
"code": 4,
"message": "Serialization error. Malformed json. Check your request and parameters are valid."
}
}'
Which I assuming it' an escape issue?
Can anyone offer any insight? Again, I thought this would be the easy part. I'm using jq to build my request json. Other alternatives to curl that can better deal with json? I'm limited to bash (which is what I know)
Thanks
To integrate (arbitrary) data into a URL (or URI in general) you need to prepare it using percent-encoding first.
As you have mentioned to use jq to compose that data, you could add #uri to your jq filter to perform the encoding, and use the --raw-output (or -r) option to then output it properly.
For example, jq -r '#uri' applied to your JSON
{
"selection": {
"selectionType": "registered",
"selectionMatch": null,
"includeRuntime": true,
"includeSettings": true
}
}
would output
%7B%22selection%22%3A%7B%22selectionType%22%3A%22registered%22%2C%22selectionMatch%22%3Anull%2C%22includeRuntime%22%3Atrue%2C%22includeSettings%22%3Atrue%7D%7D
which you can use within the query string ?json=….

How to pass variables as input to SOAP webservice from command line

I have a Soap web service to which I need to pass input as username and password to get the response. I am able to get the response from command line (Linux) if I hardcode the values with the below command.
curl --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction:ACTION_YOU_WANT_TO_CALL" --data #FILE_NAME URL_OF_THE_SERVICE
(FILE_NAME is basically my web service with hardcoded values of username and password)
But I don't want to hardcode any values and wanted to pass them as a parameters. Is there any way I can get the response from web service from command line by passing parameters to the web service?
Write a wrapper script, which 'generates' the output file with the substituted variables.

Obtaining Authorization Code from Spring OAuth 2.0 Authorization Server programmatically

I am trying to obtain an authorization code from a Spring OAuth 2.0 authorization server using simple CURL command.
curl -v --header "Authorization: Basic hasfhfashfakhsfakhf712641246" "http://0.0.0.0:0000/oauth-server/oauth2/authorize?response_type=code&client_id=dummyclient&client_secret=dummyclient&redirect_uri=http://oauth2server/oauth2callback/"
However, instead of getting back a redirect URL with the code, I am getting back a 302 redirect response to the login URL. Since I am already sending the username, password in the Authorization Header, is there a way to skip the login page redirect and get the Authorization Code directly?
you might need to tell curl to follow redirect with the -L flag
curl -L -v --header "Authorization: Basic hasfhfashfakhsfakhf712641246" "http://0.0.0.0:0000/oauth-server/oauth2/authorize?response_type=code&client_id=dummyclient&client_secret=dummyclient&redirect_uri=http://oauth2server/oauth2callback/"

Google App Scripts curl authorization

Just trying to play with google app scripts. In anonymous mode things seem fine. Except that anyone can call my script simply like that snippet shows:
curl "https://script.google.com/macros/s/.../exec?ip=\"$myIp\""
I used this manual for tips on how to authenticate through GoogleLogin. The problem is "401 Unauthorized" I received when sent auth token and "Me(owner)/Only myself" options were set on google side. (The token seems correct itself. If I omit password or mistype it, then I receive "Bad auth") If I set "Anyone, even anonymous" again, it works, but auth stuff seems like ignored. What's the correct way to do the trick?
#!/bin/bash
gmail=$1
password=$2
myIp=$3
GoogleAuthToken=""
GoogleAuthToken=`curl --silent https://www.google.com/accounts/ClientLogin --data-urlencode Email=$gmail \
--data-urlencode Passwd=$password -d accountType=GOOGLE -d source=YouDontSay -d service=lh2`
echo $GoogleAuthToken
GoogleAuthToken=$(echo "$GoogleAuthToken" | grep 'Auth=' | sed s/Auth=//)
echo $GoogleAuthToken
curl -L --silent --header "Authorization: GoogleLogin auth=$GoogleAuthToken" "https://script.google.com/macros/s/.../exec?ip=\"$myIp\""
You use ClientLogin
https://www.google.com/accounts/ClientLogin
This is google error :
Important: ClientLogin has been officially deprecated since April 20,
2012 and is now no longer available. Requests to ClientLogin will fail
with a HTTP 404 response. We encourage you to migrate to OAuth 2.0 as
soon as possible.

How do I execute an HTTP PUT in bash?

I'm sending requests to a third-party API. It says I must send an HTTP PUT to http://example.com/project?id=projectId
I tried doing this with PHP curl, but I'm not getting a response from the server. Maybe something is wrong with my code because I've never used PUT before. Is there a way for me to execute an HTTP PUT from bash command line? If so, what is the command?
With curl it would be something like
curl --request PUT --header "Content-Length: 0" http://website.com/project?id=1
but like Mattias said you'd probably want some data in the body as well so you'd want the content-type and the data as well (plus content-length would be larger)
If you really want to only use bash it actually has some networking support.
echo -e "PUT /project?id=123 HTTP/1.1\r\nHost: website.com\r\n\r\n" > \
/dev/tcp/website.com/80
But I guess you also want to send some data in the body?
Like Mattias suggested, Bash can do the job without further tools. If you want to send data, you have to preset at least "Content-length". With variables "host", "port", "resource" and "data" defined, you can do a HTTP put with
echo -e "PUT /$resource HTTP/1.1\r\nHost: $host:$port\r\nContent-Length: ${#data}\r\n\r\n$data\r\n" > /dev/tcp/$host/$port
I tested this with a Rest API and it workes fine.

Resources