How to give language preference in Microsoft Translator? - microsoft-cognitive

Request:
curl -X POST "https://api.cognitive.microsofttranslator.com/detect?api-version=3.0" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Content-Type: application/json" -d "[{'Text':'No'}]"
Response:
[{"language":"es","score":1.0,"isTranslationSupported":true,"isTransliterationSupported":false}]
What I am able to understand is that the word "No" is same in both languages English and Spanish but is there any way to tell Translator that if conflicts occurs then prefer English.

Detect api does not support setting a preference for language when multiple languages are possible. We recommend using longer sentences to get a more accurate result.

Related

Error when I POST JSON data to wordpress site

My command is:
curl -X POST --user "admin:admin" https://website.com/wp-json/wp/v2/posts/ -H "Content-Type: application/json" -d {"title":"test","content":"Content","status":"draft"}
The response I get:
{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}
Thanks!
From my earlier comments, I have the feeling that this error is related to the -d possibly needing to have quotes around the data text.
So in your case, it might be worth trying to put single quotes around the -d data text because you are currently using double quotes in the json text.
Thus, your curl command might look like this:
curl -X POST --user "admin:admin" https://website.com/wp-json/wp/v2/posts/ -H "Content-Type: application/json" -d '{"title":"test","content":"Content","status":"draft"}'
This is based on how the curl docs appear to show that quotes should be used with -d related data text based on the example under the "POST (HTTP)" section of the curl documentation here. I think this is what I have done in the past, as well, but I don't have a specific personal example on me at the moment.
Update
For what it's worth, when I tried running this curl type of command on my own, I got the following error when I didn't use single quotes around the json text:
{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}curl: (3) Port number ended with 'C' curl: (3) Port number ended with 'd'
When I ran the command with single quotes around the json data, like this: -d '{"title":"test","content":"Content","status":"draft"}', the command worked and a draft was added in WordPress. I also received back a json response that looks related to the new draft post.
Another thing to check might be the credentials used with the --user part. I ended up having to use an "Application Password" that I generated for a user in my WordPress account. This is different than my user's login password. If you haven't already tried using an "Application Password" in WordPress for this part, it might be worth trying to see if that fixes your issue. I found this when I:
Logged into my WordPress website
Clicked on the Users menu item
Clicked on a user
Scrolled down to the bottom of the user info page
Clicked on the Add New Application Password button
Ok, this was a windows specific problem, I guess most people out there are running unix/linux clients so this doesn't apply to them. I found the solution here: https://stackoverflow.com/a/7173011/15161479
The issue is with curl on windows, the quotes need to be escaped. I also did a couple other things like installing the "Application Passwords" plugin.
This is what my command looks like now
curl --user "user:application password" http://website.com/wp-json/wp/v2/posts/ -H "Content-Type: application/json" --data "{"""title""":"""test""","""content""":"""Content""","""status""":"""draft"""}"
Hope this helps some other people out there!

How to convert Curl to url with headers

I have this command in cURL and it works
curl -X GET \
-H "X-Parse-Application-Id: APP_ID" \
-H "X-Parse-REST-API-Key: API_KEY" \
-G \ https://parseapi.back4app.com/classes/Books
I want create a url that will execute the same way on the browser.
The website that I'm working with is back4app.
There is no way to achieve the same thing with just a URL. This relies on HTTP Headers (both -H parameters) that can't be translated to something else easily. To set these headers in a web browser, you'd at least need to execute JavaScript.
There might be a way if the target API supports reading the same fields from the url in some way (technically, there's no reason not to do this). I haven't found something on that topic in their docs, though.

curl ignore --data starting with # sign: don't read from file

In slack you can script slackbot to post messages to a channel like this:
curl --data "$msg" $'https://<yourteam>.slack.com/services/hooks/slackbot?token=<yourtoken>&channel=#random'
Now i'd like to mention a username as the first part of the message like msg="#joernhees hello self".
The problem with this is that if the --data argument of curl starts with an # sign it will interpret the string after the # as filename and post its content. Is there a way to make curl ignore the # sign and to send a literal # as the first char of a post request?
If you are on a new version of cURL you can also use the --data-raw option:
http://curl.haxx.se/docs/manpage.html#--data-raw
A word of warning is that looking my laptop it appears Yosemite ships with an older version of cURL.
In general if you're creating tools to post to Slack I'd recommend using an HTTP library in your script rather than calling out to a shell and invoking the curl command.
Actually i just found out i can do this (not sure it's the best option though):
curl --data '#-' $'https://<yourteam>.slack.com/services/hooks/slackbot?token=<yourtoken>&channel=#random' <<< "$msg"
The trick is to tell curl to read from stdin #- and then pass the message in via that.

What is the RCurl format to transfer a data file?

I am a bit puzzled because I can not get this to work from within R.
I want to execute the following Curl request from within R using RCurl:
curl -T 'temp.csv' -H 'Content-Type: text/csv' https://api.crowdflower.c.json?key={APIKEY}
That is, I want to transfer the file "temp.csv" to the server. This curl command itself works really well, however, I would like to send it from within R without having to use
system("curl -T 'temp.csv' -H 'Content-Type: text/csv' https://api.crowdflower.c.json?key={APIKEY}")
I have played around with the fileUpload() function, but it does not work.
Do you have any clues or hints for me how I can get this to work with RCurl? Maybe it is not really designed to be able to handle this as I see that most people use it only to automate forms or to scrape websites using R.
Thanks a lot for your help,
best Thiemo

How to send line break with curl?

I've tried the following to send a line break with curl, but \n is not interpreted by curl.
curl -X PUT -d "my message\n" http://localhost:8000/hello
How can I send a line break with curl?
Sometimes you want to provide the data to be sent verbatim.
The --data-binary option does that.
Your shell is passing \ followed by n rather than a newline to curl rather than "my message\n". Bash has support for another string syntax that supports escape sequences like \n and \t. To use it, start the string with $' and end the string with ':
curl -X PUT -d $'my message\n' http://localhost:8000/hello
See ANSI-C Quoting in the Bash Reference Manual
There's a much easier way!
curl -X PUT -d $'my message\n' http://localhost:8000/hello
This will use ANSI-C Quoting to insert the newline character.
No piping, no data files. See also Sending Newlines with cURL.
The solution for someone who doesn't want to use files, and doesn't want to resort to shell escaping magic is:
curl -X POST --data-binary #- http://url.com <<EOF
line one
line two
EOF
But this is literal newlines in the post data payload, and not in form fields.
Had similar issue. While uploading a CSV file from Mac to cloud storage, new lines were being removed. After downloading it, the entire file looked like a single line. I tried adding different EOL characters \n \r \r\n with no success. Using --data-binary instead of -d solved the issue.
Btw this issue occurred only from Mac. -d worked just fine while making the call from CentOS machine. This very much looks like due to Mac's newline character. But don't feel like debugging any more.
Thanks a lot for your help.
curl -X PUT -d #filename.csv https://cloudstorage -H "content-type: text/csv"
vs
curl -X PUT --data-binary #filename.csv https://cloudstorage -H "content-type: text/csv"
(I ended up here with a slightly different question, so I'm just going to post my answer because it might help future explorers)
My solution applies to people who are sending form-style data, i.e. key/value pairs in a query string. Use the encoded line break, which is %0A, just like how an encoded space is %20. You can use http://meyerweb.com/eric/tools/dencoder/ to convert other symbols.
So if you want to set the key message to the value:
line one
another
you would send
curl --data "message=line%20one%0Aanother" http://localhost:8000/hello
A very easy way, just Shift-Enter in the console for the break. Very readable typing it in too.
curl -d "line1
line2" http-echo.com
Server gets this: line1\nline2
Do this to remove the line break:
curl -d "line1 \
line2" http-echo.com
Server gets this: line1 line2
Not an answer to your question, but I would work around it by creating a temporary file containing the message and line break, and give curl that file to work on:
curl -X PUT -d #message.txt http://localhost:8000/hello
From the manual:
If you start the data with the letter #, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. The contents of the file must already be URL-encoded. Multiple files can also be specified. Posting data from a file named 'foobar' would thus be done with --data #foobar.
I was using Sendgrid with this code (copied below) originally found here https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html
\n\n worked in Gmail, but \n was ignored. I tried to double the escape and other suggestions. I also tried \r\n and that did not work in Gmail either. Note: I didn't bother to test other email clients, maybe it was a Gmail-specific problem.
curl --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{"personalizations": [{"to": [{"email": "your.email#example.com"}]}],"from": {"email": "example#example.com"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Heya!"}]}'
Eventually I gave up looking for a solution and switched the text/plain to text/html and just used <br /> tags.
Someone suggested that Sendgrid converts plaintext to HTML if you have a tracking pixel enabled, which makes sense. Maybe the newlines were destroyed in the plaintext-to-html conversion process. I assume the client wants a tracking pixel, so decided to switch to HTML.

Resources