How can this curl command be converted to pycurl or python requests? - api

any hel pwill be highly appreciated. Just can't make this to work. I am basically trying to update my Grafana dashboard via a http request. managed to get it to work with curl but want to do this with python's requests or pycurl.
curl -X PUT https://<api token>#ks.hostedgraphite.com/api/v2/grafana/dashboards/<my_dashboard> --data-binary #dashboard.json
The command above works. Tried several ways, an example of a code snippet:
crl = pycurl.Curl()
crl.setopt(crl.URL,
'https://apitoken#ks.hostedgraphite.com/api/v2/grafana/dashboards/<my_dashborad>')
crl.setopt(crl.UPLOAD, 1)
file = open('dashboard.json')
crl.setopt(crl.READDATA, file)
crl.perform()
crl.close()
file.close()
print('Status: {}'.format(crl.getinfo(crl.RESPONSE_CODE)))

curl -X PUT https://api_token#ks.hostedgraphite.com/api/v2/grafana/dashboards/my_dashboard --data-binary #dashboard.json
translates to
import requests
data = open('dashboard.json', 'rb').read()
response = requests.put('https://api_token#ks.hostedgraphite.com/api/v2/grafana/dashboards/my_dashboard', data=data)
Just replace proper values for api_token and my_dashboard.
You can use https://curl.trillworks.com/ for converting curl commands to equivalent code using python requests.

ok, so my main issue was creating a http request via python requests.
Figured it out, I setthe auth param to auth=("graphite_api_token","") and it worked:
data = json.dumps(dashbord_dict)
headers = {"Accept": "application/json","Content-Type":"application/json"}
response =requests.put('https://ks.hostedgraphite.com/api/v2/grafana/dashboards/some_dashboard,auth=("graphite_api_token",""),data=data,headers=headers)
print(response.status_code)

Related

golang passing data payload from HTTP client

I have an HTTPS endpoint that works fine with a cURL command like this:
curl -k -u xyz:pqr \
--data "grant_type=client_credentials" \
https://my-url
Now I am trying to use golang to call the same API endpoint like this:
data := url.Values{}
data.Set("grant_type", "client_credentials")
req, err := http.NewRequestWithContext(
ctx,
"POST",
"https://my-url",
strings.NewReader(data.Encode())
)
It fails with the error body:
{
"error_description":"grant_type is required",
"error":"invalid_request"
}
I am already passing grant_type as the last argument to the http.NewRequestWithContext() function.
What am I missing?
You are not translating the curl correctly. This tool might be helpful: https://mholt.github.io/curl-to-go/
Essentially you need to add user auth.
req.SetBasicAuth("xyz", "pqr")
// Might help to set the `Content-Type` too
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
-k on curl seems to be reading some curl arguments from a file too. You are going to want to make sure you translate those over as well. I cannot see what they are from your example.

How to query SFTP server with Karate via API?

The app that I am trying to test has an SFTP server that can be queried via API. Swagger shows the following sample cURL request to get domain files information, and I am having a hard time making this call with Karate:
curl -X GET -H 'Accept: application/json' 'https://{host}:{port}/api/{clientId}/'
I do the following where baseUrl is defined as https://sftp.mydomain.com:22 where 22 is the port number that I can successfully use to connect to the SFTP server via Cyberduck:
Feature:
Background:
* url baseUrl
* def moduleBase = '/api/12345/'
Scenario:
* path moduleBase
When method get
Then status 200
The error that I get is this:
ERROR com.intuit.karate - src/test/java/mytest.feature:9
When method get
http call failed after 815 milliseconds for url: https://sftp.mydomain.com:22/api/12345/
What am I doing wrong?
Clearly it may not be HTTP so I don't think Swagger and all is legit.
Maybe you can just delegate to the OS. Refer: https://stackoverflow.com/a/64352676/143475

Configure Karate Mock to work with API's having Content Type other than JSON, XML

I need to mock an API using Karate Mock which accepts messages with Content-Type as below and respond accordingly
'Content-Type: application/x-www-form-urlencoded'
I tried to configure it in feature file as below but encountered below issue. I guess by default it is accepting JSON as the content-type, Is there any way to configure mock to read custom messages like this one.
Actual API call:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "elem_value=generic_value&address=" http://myhost:9090/api/resource_path
Mock feature file:
Scenario: pathMatches('/api/resource_path') && typeContains('urlencoded')
* def responseStatus = 200
* def response = read ('/payload/Success.json')
* print response
Actual Error:
Caused by: <eval>:1 ReferenceError: "generic_value" is not defined
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)
at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291)
at jdk.nashorn.internal.objects.Global.__noSuchProperty__(Global.java:1441)
at jdk.nashorn.internal.scripts.Script$43$\^eval\_.:program(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
This should have been fixed in a newer version of Karate, please try upgrade.
I think it will be in 0.9.2 - https://github.com/intuit/karate/issues/649

How to update gitlab merge request with curl? Getting 401 all the time

Need to update labels in merge request during gitlab ci. Tried different variants:
curl -X PUT https://gitlab.com/api/v4/projects/{project_id}/merge_requests/134?access_token={token}&labels=merged
Tried passing token as header and as data.
Tried passing "personal_token", "access_token", "token" and etc.
Getting {"message":"401 Unauthorized"} all the time.
I think that you should use private_token as it said on documentation:
curl https://gitlab.example.com/api/v4/projects?private_token=<your_access_token>
or in the header, as you said:
curl --header "PRIVATE-TOKEN: xxx" "https://gitlab.com/api/v4/projects"
Have a look on the official page:
https://docs.gitlab.com/ee/api/README.html#personal-access-tokens
Example:
Getting information from merge request with iid 1(the actual labels are testlabel and testlabel3):
#curl -X GET https://gitlab.com/api/v4/projects/11209705/merge_requests/1?private_token={XXXXXX}
{"id":28761206,"iid":1,"project_id":1120970.......,"labels":["testlabel","testlabel3"],...}
Sending put with curl with "--data" and the parameter to update "labels" to "test":
# curl -X PUT https://gitlab.com/api/v4/projects/11209705/merge_requests/1?private_token={XXXXX} --data "labels=test"
Getting information from merge request with iid 1(the actual labels is "test"):
#curl -X GET https://gitlab.com/api/v4/projects/11209705/merge_requests/1?private_token={XXXXXX}
{"id":28761206,"iid":1,"project_id":1120970.......,"labels":["test"],...}

NiFi- how to http post a PDF document

I wanted to use NiFi's posthttp/invokeHttp processor to post a PDF to an API.
But considering the following cURL request to replicate in NiFi:
curl -X POST "http://ipaddress:port/api/" -H "accept: application/json" -H
"Content-Type: multipart/form-data" -F "pdf_file=#sample.pdf;
type=application/pdf"
Which property takes the -F information in nifi attributes?
Configuration for invokehttp right now:
error:
"400 Bad Request: The browser (or proxy) sent a request that this server could not understand."
Configration for posthttp right now:
error:
server logs: readv() failed (104: Connection reset by peer) while reading upstream
In older version of nifi you will have to use your own script to build a multipart request and then use invoke to create post request. You can refer to this post for a ExecuteGroovyScript example.
https://stackoverflow.com/a/57204862
Since Nifi 1.12 you can directly use invokeHTTP by setting content-type
https://stackoverflow.com/a/69284300
When you use PostHttp/InvokeHttp you wouldn't be referencing an external file, you would be sending the content of the flow file. So you would first need to bring sample.pdf into NiFi by using GetFile or ListFile/FetchFile and then flow file coming out of those processors represents the PDF, and you would route that flow file to InvokeHttp which would POST the content of the flow file (the pdf).