I have this curl URL to connect to couchDB.
curl -X PUT http://admin:ulacit#13.90.93.32:5984/test/"001" -d '{"name":"moises"}'
I saw a lot of questions with GET and POST but I didn't find an example with PUT.
Am I doing this the right way?
$.ajax({
crossOrigin: true,
url : 'http://admin:ulacit#13.90.93.32:5984/test/'+user,
type : 'POST',
processData: false,
dataType: 'json',
contentType: 'application/json',
data: {pass:pass,email:email},
success:function(result){
if(result!="error"){
alert("Registro Correcto, Proceda a entrar");
open("login.html","_parent" );
}else{
alert("Usuario Ya Utilizado");
}
},
The PUT HTTP verb is used to either:
create a database e.g. curl -X PUT http://localhost:5984/mydb
create a document in a database when you know the id of the document you wish to create e.g.
# create a document with PUT with document id in the URL
curl -X PUT -d '{"a":1,"b":2}' -H 'Content-type: application/json' http://localhost:5984/mydb/mydocid
{"ok":true,"id":"mydocid","rev":"1-25f9b97d75a648d1fcd23f0a73d2776e"}
which is the equivalent of:
# create a document with POST with the document id in the body
curl -X POST -d '{"_id":"mydocid","a":1,"b":2}' -H 'Content-type: application/json' http://localhost:5984/mydb/mydocid
Related
I consistently receive the response of 'operation not found' when I send a GraphQL query or mutation in Cypress. Taking tips from this post https://stackoverflow.com/a/68750026/16958500 I have the following in my it block (its working in Postman)
it('Retrieve and Store Session Token', () => {
const query = `mutation {
requestSMSSessionToken(phoneNumber: "1666555021"){
cookieKey
tokenValue
expires
}
}`;
cy.request({
method: 'POST',
url: '/',
headers: {
'Content-Type': 'application/json',
},
body: { query },
}).then((res)=>{
cy.log(JSON.stringify(res))
expect(res.status).to.eq(200)
});
})
Here is my Postman Curl Code Snippet (that's working)
curl --location --request POST '/' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation {\r\n requestSMSSessionToken(phoneNumber: \"1666555021\"){\r\n \r\n cookieKey\r\n tokenValue\r\n expires\r\n \r\n }\r\n }","variables":{}}'
any tips to share to get this working?
My API responds to the following curl request:
curl --data "session[email]=email#gmail.com&session[password]=password" -H 'Accept: application/vnd.my-app.v1' \http://my-app.dev/sessions
But not to this Ajax:
$.ajax({
url: '/sessions',
method: 'POST',
headers: 'Accept: application/vnd.superstar-is-back.v1',
data: {
session: {
email: this.email,
password: this.password,
},
}
})
Am I getting something wrong with this syntax?
I'm not sure how this impacts Rails Routing, but based on the jQuery docs, I think the headers property needs to be an object: headers: { Accept: 'application/vnd.superstar-is-back.v1' }
I am struggling to call an external API which is supported only for XML using HTTP Request (parse.cloud.httprequest). The cURL request format is :
curl https://core.spreedly.com/v1/gateways/UD0s1PcwgI55XJefzrlBBQMwaUj/purchase.xml -u '60lRz6BfKxHESwJaVSf4kiZpu5O:xVqRwqem0KEuthvE9n8aIE1Al8FVL2QwdC2z8cS8jQYXAQER18wMW8SD6BhOF4fX' -H 'Content-Type: application/xml' -d 'YsTXXDhYwePyMW8KlgeOmkZ1qlR100USD'
This (above) works fine. Now I need to call this XML API from Parse cloud function. The parse documentation on HTTP Request shows only examples of JSON not XML. Here is what I am using in HTTP Request cloud method but it is failing with 401 error.. Please help.
Parse.Cloud.httpRequest({
method:'POST',
url: url1,
headers: {
'Content-Type': 'application/xml',
'Authorization' : "'"+environment_key+":"+access_secret_key+"'"
},
data: "<transaction><payment_method_token>cust_token</payment_method_token><amount>amount</amount><currency_code>currency_code</currency_code></transaction>"
}).then(function(httpResponse) {
var response1=httpResponse.data;
console.log("res :"+response1);
response.success(response1);
}, function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
});
I need to convert this to ajax.
curl -X POST \
-H "X-Parse-Application-Id: ****************" \
-H "X-Parse-REST-API-Key: ******************" \
-H "Content-Type: application/json" \
-d '{
"deviceType": "ios",
"deviceToken": "result" ,
"channels": [
""
]
}' \
https://api.parse.com/1/installations
I found the solution is to create an ajax post method. So in this example, you would do the following ajax method.
var headers = {
"X-Parse-Application-Id":"Your ID",
"X-Parse-REST-API-Key":"Your Rest API Key"
};
// Convert data object to JSON string:
var userData = { "deviceType": "the device or the variable that says the device",
"deviceToken": "The Token itself or the variable with the token",
"channels": [""]
};
var data = JSON.stringify(userData);
$.ajax({
headers: headers,
type: 'POST',
url: "https://api.parse.com/1/installations",
contentType: "application/json",
data: data,
dataType:"json",
success:function(data) {
// We log the responce from the server just to check.
alert("data:" + JSON.stringify(data) + " status: " + status);
},
error:function(data) {
// Show error message:
alert("Your data didn't save!" + JSON.stringify(data));
}
});
i'm using play framework 2.0.4
i have a route :
POST /addMail controllers.Application.addMail()
In my controller Application i define the addMail method :
public static Result addMail()
{
JsonNode json = request().body().asJson();
Long id = json.findPath("id").asLong(0);
String email = json.findPath("email").getTextValue();
GameScore gs = GameScore.findById(id);
gs.setEmail(email);
gs.save();
return ok();
}
If i call this method through CURL i have no problem :
curl --header "Content-type: application/json" --request POST --data '{"id": 13, "email": "test#DB.com"}' http://localhost:9000/addMail
But if i call this method through an AJX request i have a 500 response.
$addMailBtn.click(function(event) {
$this = $(this);
var id = $this.attr("id").substring(14);
var email = $("#saisieMailField_" + id).val();
$.ajax({
type: 'POST',
url: "#routes.Application.addMail()",
dataType:"json",
data: {"id":id, "email": '"' + email + '"'},
success: location.reload()
})
} );
If i print in my console my json data, json data is null when i perform my ajax request but is alright through curl.
I have tried to add
#BodyParser.Of(play.mvc.BodyParser.Json.class)
on my method but it doesn't change anything.
Thanks for your time.
This works for me. Note that i stringify the JSON object, and I think this is your problem.
$.ajax({
type: "POST",
url: "http://myservice:9000/api/v1/positions",
data: JSON.stringify({"nwLng":72.22,"nwLat":22.22, "seLng":22.22,"seLat":55.33}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) { alert(data); },
failure: function (errMsg) { alert(errMsg); }
});