How do you call non-write functions from a module without needing to pay gas, using pact-lang-api? - pact-lang

I have a few simple functions that get data from the blockchain, which normally does not require sending a transaction when using Chainweaver.
(defun get-price (price-key:decimal)
(at "price" (read price-table price-key ["price"]
)
This function does not change any data, so it shouldn't require gas. How can I use something like the x-wallet browser plugin to call this function and just get the data?

You can just call the local API via HTTP request.
I.e.
localRes = await fetch(
`${networkUrl}/api/v1/local`,
{
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify(cmd),
};
);
Note: For the cmd - you need to make a valid pact-lang-api command which specifies the smart contract and method to call

Related

fetch function returns 400 bad request error for content-type 'application/x-www-form-urlencoded'

This question has been asked before but those solutions i have already tried and i shall
explain along the way.
Task:
I want to send data to api with content-type as "application/x-www-form-urlencoded".
Problem:
The URL works fine in Postman and returns me the correct response. When i try the same body parameters through my Reactjs app, it responds with 400 bad request error.
What i have tried:
myBody:{
grant_type:"XYZ",
client_id:"XYZ"
}
var request = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method: "POST",
body:myBody
};
Then i fetch something like this.
const data = yield fetch(complete_url,request);
I also tried to compose myBody as a formData . Still gives the same error.
Apologies i have not given the URL because of restrictions. I am looking for possibilities of errors i could make.
With 'Content-Type': 'application/x-www-form-urlencoded',
Your request body should follow certain rule (https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST).
the keys and values are encoded in key-value tuples separated by '&', with a '=' between the key and the value,
example:
grant_type =value1&client_id=value2.
So i suggest what it looks like in debugger -> network tab.
If the request body is not a problem. Then you should check if there's any special chars.
You also will need to escape symbols and special chars.

How do I make axios post request with body and form data simultaneously?

I want to send a post rquest with axios that has body data and form data and I can get anything to reach through to my route hadler:
axios({
method: 'POST',
url: `${keys.SERVER_URL}/post/new-post/${isAuthenticated()._id}`,
formData,
data: { title, content },
headers: {
'Content-Type': 'application/json',
'Content-Type': 'multipart/form-data',
},
});
then in the route handler I console.log and I get nothing:
app.post("/new-post", (req, res) => {
console.log(req.body);
console.log(req.file):
});
There is no formData option for the axios() options object. If you want to set formData, you set the data property to your formData.
So, that illustrates that there is ONLY one set of data that you send with a POST or a PUT. If you want to send multiple pieces of data, you have to combine them into the same set of data and you have to obviously make them the same type of data (since there is only one master content-type). If you really needed to send multiple different types of data, then you would need to send a multi-part body that has separate sections to it. You would likely need some sort of helper to create that multi-part body since that isn't something that axios will do for you and it can get a little complicated.
If you could describe what you're actually trying to do (what the actual purpose of this request is), then we might be able to advise how to combine your data into one simpler type so it an be sent more simply.

Angular: set Content-Type per query

I need to perform one query with Content-Type different than application/json.
Changing default $http options is not a variant, because a lot of queries still be performed with JSON data.
I've found example
var req = {
method: 'POST',
url: 'example.com',
headers: {
'Content-Type': "application/x-www-form-urlencoded"
},
data: "somedata"
}
$http(req).then(function (resp) { console.log(resp);});
But it don't want to work — Content-Type is still a application/json.
Is any true way to do it right? Changing $http defaults and then restoring is not a solution.
What you are doing is not a valid way to set headers in angularjs.
To add or overwrite these defaults, simply add or remove a property
from these configuration objects. To add headers for an HTTP method
other than POST or PUT, simply add a new object with the lowercased
HTTP method name as the key, e.g. $httpProvider.defaults.headers.get =
{ 'My-Header' : 'value' }.
Ref. https://docs.angularjs.org/api/ng/service/$http - "Setting HTTP Headers" section
Edit.
What you're doing is right but you're missing one thing.
In your scenario it does not work because when using application/x-www-form-urlencoded you need to encode your data using $httpParamSerializerJQLike(). Also remember to put $httpParamSerializerJQLike as dependency.

I am uploading the image file in Angular Js to call the java api but my form data is not hitting the api

controller:
$scope.fileToUpload = function(input) {
if (input.files && input.files[0]) {
CommonService.uploadContactImage.upload({
fileName : input.files[0].name
}, input.files[0], function(data) {
});
}
}
Service:
uploadContactImage:function(input){
console.log("game image");
var req = $http({method: 'POST', url: options.api.base_url + '/gameimageupload/',
dataType: 'json', headers: {'Content-Type': undefined}})
.success(function (data)
{
console.log("data" + data);
return data;
});
If you take a good look at your code you will see that there are quite a few things wrong with it. For example, you have defined in your service an uploadContactImage function which takes a single Javascript object as argument (input), while in your controller you attempt to call CommonService.uploadContactImage.upload(...) instead of CommonService.uploadContactImage(...). Additionally, even if the uploadContactImage function was called correctly it doesn't actually do anything with its argument, ie. the input object is never used in the function body.
These issues aside you cannot submit a file to the server just by adding it to the body of a POST request the way you (seem to be) trying to do. Without going into too much detail here, in order to upload a file from the browser a request with content type multipart/form-data needs to be submitted, which will contain your file as well as the necessary HTTP headers for the server to identify it and parse it correctly. I suppose you could try and construct this request yourself, however it's not a task for the faint-hearted. What I would suggest instead is to use one of the many file upload modules available for Angular.js. A Google search will give you quite a few modules that you can check out to see which better fits your needs.

"Failed to parse UUID" error message on attempting to login via TrueVault api

On attempting to login via the truvault api using angular js, I am getting this error message: Failed to parse UUID. I am passing the username, password and account_id as params. I am successful using the curl command and get the success response.
The 400 error is not described in the api docs for authorization. I am not sure about if this UUID is linked to the schema_id. Would anyone (truevault guys!!) know what I am doing wrong?
I contacted truevault support on this one. Dan helped me get through it.
I was passing the username/password/account_id as url string query parameters. I had to make two changes to the code:
1. Pass the above as form data parameters
2. add the angular-post-fix.js to my project.
(Note: I am not adding the link as there are editors who will disallow the post with links to elsewhere. It has happened in the past!)
When using Node.js, the querystring API is really useful. Just pass an object to the querystring.stringify() function, and the resulting output is ready to be sent to TrueVault for login.
Additionally, I found that adding the header 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' might be necessary (which is one of the things the Angular post-fix library does).
#orthodoc is right, but is kind of tricky how to actually build the request. Lets say we are using fetch with formData params, I'd like to add an example of a successful request:
...
var formData = new FormData();
formData.append('username', username);
formData.append('password', password);
formData.append('account_id', accountId);
return fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
},
body: formData
});
...

Resources