Convert curl command into a working script - python-3.x

I am using an API from check-host.net to ping an website.
My issue is right now that I have no ideea how I could transform the curl command api into an working python script. I tried different approaches which I found on here but sadly none has give me the ouput I am looking for.
Working curl command:
curl -H "Accept: application/json" \ https://check-host.net/check-tcp?host=smtp://gmail.com&max_nodes=1
the respons looks something like that:
{ "ok": 1, "request_id": "29", "permanent_link":
"https://check-host.net/check-report/29", "nodes": {
"7f000001": ["it","Italy", "Marco"] } }

You have to send a Accept: application/json header in your request. You can also use the builtin json decoder in requests.
import requests
headers={
'Accept': 'application/json'
}
r=requests.get('https://check-host.net/check-tcp?host=smtp://gmail.com&max_nodes=1',headers=headers)
print(r.json())
Output
{'nodes': {'us2.node.check-host.net': ['us', 'USA', 'New Jersey', '199.38.245.218', 'AS394625', 'OK']}, 'ok': 1, 'permanent_link': 'https://check-host.net/check-report/a462c3ck399', 'request_id': 'a462c3ck399'}

Related

Unable to get data using requests.get while it works with curl

Im trying to get response using RESTCONF. When i use CURL as:
curl -X GET -H "Accept: application/yang-data+json" -H "Content-Type: application/yang-data+json" -k https://user:pass#IP/api/data/switch:opm/port={1,2}
I get output perfectly as
"switch:port": {
"port-id": 1,
"wavelength": "0000.0",
"offset": "0.0",
"averaging-time": 4 } } { "switch:port": {
"port-id": 2,
"wavelength": "0000.0",
"offset": "0.0",
"averaging-time": 0 } }
But when i use requests.get in python as:
url ="""https://IP/api/data/switch:opm/port={1,2}"""
response = requests.get(url,headers=self.header,auth =(self.username1,self.password1),verify=False)
I get response 404.
I tried port=1 and it works fine in python too. Facing issues when i use port={1,2}.

I am getting "TypeError: openai.createEdit is not a function" while trying to use the Create edit function, can anyone point if there's a mistake?

I tried the the same in a node.js web app and in postman, I have added the code and curl for respectively below:
Node JS:
const response = await openai.createEdit("text-davinci-002", {
input: "What day of the wek is it?",
instruction: "Fix the spelling mistakes" });
CURL:
curl --location --request POST 'https://api.openai.com/v1/engines/text-davinci-002/edits'
--header 'Content-Type: application/json'
--header 'Authorization: Bearer API_KEY'
--data-raw '{
"input": "What day of the wek is it?",
"instruction": "Fix the spelling mistakes"
}'

Github API - add an issue to a project?

Is it possible, using the github API, to add an Issue to a project board?
This document:
https://docs.github.com/en/rest/reference/projects#create-a-project-card
suggests it is possible, but my attempts using https://github.com/fastai/ghapi have failed.
I have tried:
api.projects.create_card(col.id, note=f'{issue.title} {issue.html_url}', content_id=issue.id)
but the card does not show up as an issue card, it shows up as a card referencing an issue - which is different.
I also tried:
curl -X POST -H "Accept: application/vnd.github.v3+json" https://api.github.com/projects/columns/####/cards -d '{"column_id": colid, "content_id": cid, "content_type": "json"}' -u user:$GITHUB_TOKEN
and that gave an error:
{
"message": "Validation Failed",
"errors": [
{
"resource": "ProjectCard",
"code": "unprocessable",
"field": "data",
"message": "Could not resolve to a node with the global id of 'XXXXXXXXXX'."
}
],
"documentation_url": "https://docs.github.com/v3/projects/cards/#create-a-project-card"
}
which suggests that I've mucked up the curl request, but I'm not sure where.
I've found a solution following https://stackoverflow.com/a/57696731/814354. I need to declare the content_type in the POST request (which is information I was unable to find on the github documentation).
CURL version:
curl -X POST -H "Accept: application/vnd.github.v3+json" https://api.github.com/projects/columns/{column_id}/cards -d '{"column_id": {column_id}, "content_id": {issue_id}, "content_type": "Issue"}' -u {username}:$GITHUB_TOKEN
The ghapi version does not work:
# this fails - `content_id` and `content_type` are not included in the request
api.projects.create_card(col.id, content_id=issue.id, content_type='Issue')
but you can still use the ghapi in a clunkier way:
api(path=f'/projects/columns/{col.id}/cards', verb='POST',
data={'content_id': issue.id,
'column_id': col.id,
'content_type': 'Issue'
},
headers={"Accept": "application/vnd.github.v3+json"}
)

Error in Xray Rest API call for importing Test Execution Result

I know this query has been answered in so many post but those have not helped me. I did research, and tried, but still facing issue in making an API call to import test execution result.
Approach I took:
Created Test(Test Details: Cucumber), Test Precondition, Test Set, Test Plan and Test Execution
Exported Test using "Xray - Export to Cucumber" option
Added this in my BDD-Cucumber framework, executed and it has generated me cucumber.json file after execution
Trying API call using postman
/api/v1/import/execution/cucumber
curl --location --request POST 'https://xray.cloud.xpand-it.com/api/v1/import/execution/cucumber' \
--header 'Authorization: Bearer $token’ \
--header 'Content-Type: application/json' \
--data-binary '#/Users/aranjan/Downloads/cucumber.json'
Error:
{ "error": "Error creating Test Execution - Team is required."}
Now, this means it is trying to create new instead of update existing
Then, I used
/api/v1/import/execution/cucumber/multipart
curl --location --request POST 'https://xray.cloud.xpand-it.com/api/v1/import/execution/cucumber/multipart' \
--header 'Authorization: Bearer $token’ \
--form 'info=#/Users/aranjan/Downloads/xrayresultimport.json' \
--form 'result=#/Users/aranjan/Downloads/cucumber.json'
Error:
{ "error": "Unexpected field (result)"}
xrayresultimport.json
{
"fields": {
"project": {
"key": "HYP"
},
"customfield_10962": [
"Team","TeamQAAuto"
],
"issuetype": {
"id": "10722"
}
}
}
/api/v1/import/execution
curl --location --request POST 'https://xray.cloud.xpand-it.com/api/v1/import/execution' \
--header 'Authorization: Bearer $token’ \
--header 'Content-Type: application/json' \
--data-raw '{
"testExecutionKey": "HYP-3313",
"info" : {
"startDate" : "2020-09-25T11:47:35+01:00",
"finishDate" : "2020-09-25T11:53:00+01:00",
"testPlanKey" : "HYP-3341"
},
"tests" : [
{
"testKey" : "HYP-3330",
"start" : "2020-09-25T11:47:35+01:00",
"finish" : "2020-09-25T11:50:56+01:00",
"comment" : "Successful execution",
"status" : "PASSED"
}
]
}'
{ "error": "Error updating Test Execution - Issue update failed!"}
Agenda:
I want to import the execution result in my existing Test Execution.
I request you to guide me here.
Thanks in advance.
Currently, if you use the multipart endpoint it will always create new Test Executions.
The multipart request has a minor typo: you should have "results" instead of "result". An example would be something like this:
curl -H "Content-Type: multipart/form-data" -X POST -F info=#xrayresultimport.json -F results=#cucumber.json -H "Authorization: Bearer $token" https://xray.cloud.xpand-it.com/api/v2/import/execution/cucumber/multipart
That should make it work :)
Note: concerning the last example you gave, there could be several causes for it, including restrictions on Jira side. That would be analyzed by the Xray support team.

Passing variables to curl command in child_process.exec fails

I was trying to use child_process.exec to call curl with a long command in order to send some data to an API. Something similar to the following example:
exec('git log --oneline | wc -l', function(error, stdin, stderr) {
if (stdin > 1) {
exec('curl -H "Content-Type: application/json" -X POST -d \'{"value1": "\'"$arg"\'"}\' https://https://maker.ifttt.com/trigger/{event}/with/key/<my-key>', { "env" : {"arg": stdin } });
}
})
So if a git repo includes more than one line in its git log output, then you execute a POST request to some API (here, a simple webhook in ifttt.com), in which you're passing some variable (arg) in the process.
Notice that this is the best attempt, but in general, I was struggling quite a bit to escape single and double quotes. In this particular case, the HTTP request was not sent correctly because the body includes a line break:
POST / HTTP/1.1
Host: <some-host>
User-Agent: curl/7.50.1
Accept: */*
Content-Type: application/json
Content-Length: 16
{"value1": "2
"}
At the end, I had to use an external bash script:
exec('./send_request.sh $arg', { "env": {"arg": stdin } });
but I'm still very curious on how to make it work within the same js file.
If it helps, I'm running node 6.11.0 and curl 7.52.1.
Try:
exec('git log --oneline | wc -l', function(error, stdin, stderr) {
if (stdin > 1) {
exec('curl -H "Content-Type: application/json" -X POST -d \'{"value1": "\'"$arg"\'"}\' https://https://maker.ifttt.com/trigger/{event}/with/key/<my-key>', { "env" : {"arg": stdin.replace(/\n/g, '') } });
}
})
What is happening is that your variable 'stdin' (you should rename it to 'stdout') has a \n at the end of it.

Resources