Angular $http post call passing data issue to GO backend - angularjs

I am trying to get access to backend written in GO which in 99% is good (problem does not lay there).
For now I just created simplest call which stay in controller (it will be in service in future) to register new user. Although I hardcoded data which I am passing the response says 403 forbidden. In powerShell shows reason of 403:
RegistrationForm parse - email: , nick:
Validation failed for email - blank
It looks like I am not passing my data correctly because email is blank. Please take a look at my code:
$ctrl.fake_registerSubmit = function() {
$http({
url: 'http://localhost:3000/v1/sign_up',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: 'POST',
data: {
email: 'check#onet.pl',
nick: 'borysxoxo',
password: 'admin12312',
password_confirmation: 'admin12312'
}
})
.then(function successCall(response, post) {
console.log('user added');
}, function errorCall(respone) {
console.log('error callback');
console.log(respone);
})
};
This screenshot presents API documentation which I am trying to access:
link
What am I doing wrong? Is there other way to pass data?

You are sending some json data with the wrong Content-Type.
I see 2 options, either change Content-Type in your header to:
'Content-type' : 'application/json'
or transform your payload to:
data: "email=check#onet.pl&nick=borysxoxo&password=admin12312&password_confirmation=admin12312"

Related

How to "intercept" a request that is made with cy.request with cypress

As far as I understand cy.intercept() can be used to stub requests that the application itself makes.
Now I have a HTTP POST request with cy.request() in one of my custom commands in Cypress. Because this is a request made by cy.request() function I can't use cy.intercept() to stub the response of this request.
Is there any workaround to stub a respons of a request made with cy.request() ?
Now I have the following which is logging the real response correctly, but I want to keep this response even the when the remote server is offline:
cy.request({
method: 'POST',
url: 'https://sample.com/token',
body: {
username: "UserNameSample",
password: "PasswordSample"
},
form: true,
}).then(response => {
cy.log(JSON.stringify(response.body))
})
Which is resulting in the following printscreen of the comment log in cypress.:
You can try for fetch interface to make the network calls instead:
cy.intercept({
method: 'POST',
url: 'https://sample.com/token',
},
{
// your stubbed response
}).as('createToken').then(() => {
fetch('https://sample.com/token', {method: 'POST'})
.then((response) => {
cy.log(JSON.stringify(response.body))
})
})
cy.wait('#createToken').its('response.body')
P.S. I've not tested it, so it might need some adjustments

Sending POST request weird issue

I'm quite new with ANGULAR and web development in general and I'm currently working on a web tool. Now I want this tool to send a POST request to a web service but Im encountering a weird bug. Now I have below code in my javascript:
var data_info = { test_id: 'TEST', model_id: 'TEST:TEST_ID' };
//data_info = JSON.stringify(data_info);
var request_json = {
method: 'POST',
url: url,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: data_info,
cache: false,
};
console.log(request_json);
$http(request_json).then(function successCallback(response) {
// response code here
}
Now this code currently doesn't pass the preflight request check and is always returning a 405. But if I change the line data: data_info in the request JSON into a new key let's say body: data_info it now successfully sends the request and I can confirm that the service is receiving it. I'm not sure what's the issue here and can't figure it out.
change your header to :
headers : {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
Please try

Pass Data List from angularjs to webservice?

I'm working on app in ionic 1 platform using angularjs, in which I want to Pass List of object to Web-service, How can I do it?
I have tried doing this but was not able to send any Data..
Here is my code and how to pass list of object in data: $scope.AddNew
$http({ url: $rootScope.HostName + '/bulk', dataType: 'json', method: 'POST', contentType: "application/json; charset=utf-8", data: $scope.AddNew, headers: { 'content-type': 'application/json' } }).success(function (response) { alert("Success"); }).error(function (error) { });
If there is another approach or way to do it then please do help
Thanks in advance.
Assuming your $http call is in the controller where you can access $scope.
The way you had passed is correct, but at the server side you should accept your request body as an Array of objects.
If your server side is java spring app,
You would design your method with #RequestBody YourClass[] objs
I think your code is correct, just so it is simple and readable I would suggest this format:
$http.post($rootScope + '/bulk', $scope.AddNew).then(function(response) {
alert("Success");
}, function(error) {
})
The promise structure in AngularJS has since been updated. In regard to your question, the code should work fine if you can access AddNew through your $scope. Make sure you are handling your requests properly in the backend. Try logging for checking if data is sent and received.

angular and dropbox can(t get token with code flow

i have this snippet:
var req = {
method: 'POST',
url: 'https://api.dropboxapi.com/oauth2/token',
headers: {'Content-Type': 'application/json'},
data: {
'code': authCode,
'grant_type': 'authorization_code',
'client_id': 'my_id',
'client_secret': 'my_secret',
'redirect_uri':'http://localhost%3A8080/main'
}
};
return $http(req).then(function(response){
console.log(response.status);
return response;
}, function(err){
console.log(err);
});
The can always ends up in a "bad request" because ""No auth function available for given request""
The same data works with tools to send REST requests... so I don't know what I'm missing here...
Can some help?
The error message indicates that the API didn't receive the expected parameters, or at least not in a format it expected. The documentation for /1/oauth2/token say:
Calls to /oauth2/token need to be authenticated using the apps's key and secret. These can either be passed as POST parameters (see parameters below) or via HTTP basic authentication. If basic authentication is used, the app key should be provided as the username, and the app secret should be provided as the password.
You seem to be attempting to supply the parameters as JSON though, according to your Content-Type header. Try sending them as POST parameters instead.

AngularJS send request x-www-form-urlencoded

I have a Django server amd I am trying to send a request to it using AngularJS.
This is my code:
var req = {
method: 'POST',
url: 'http://127.0.0.1:8000/api/user/register/',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/x-www-form-urlencoded'
},
data: $.param({ phone_number: phonenumber, password: phonenumber, country: 1 })
}
$http(req).then(function(response){
callback(response);
}, function(response){
console.log(response);
});
I am getting the following error:
XMLHttpRequest cannot load http://127.0.0.1:8000/api/user/register/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1' is therefore not allowed access.
I am not sure what can be the problem here. I already created an applications for iOS and Android that both communicate with the same server and I didn't have any problems.
I am using AngularJS 1.5.5 https://code.angularjs.org/1.5.5/
Can someone suggests what is wrong with this request in AngularJS?
It appears that the code didn't need this part:
'Access-Control-Allow-Origin': '*',
The real problem was because the server wasn't allowing local urls.

Resources