API request not working ReferenceError: GetAddressForRandomNftSale is not defined - api

I am trying to run a GET request using an API but I am getting a reference error code when I run the script. I really appreciate any help with how to solve this!
Thank you
ERROR MESSAGE: "ReferenceError: GetAddressForRandomNftSale is not defined"
FRONTEND CODE:
Press on click event for #button1 to display result in a textbox #result
Code below
import {GetAddressForRandomNFTSale} from 'backend/serviceModule' ;
export function button1_click(event) {
GetAddressForRandomNftSale($w("address").value)
.then(address => { $w('#result').text})
}
BACKEND CODE:
The code to fetch the GET API request using "serviceModule.jsw"
Code below...
import {fetch} from 'wix-fetch';
export function GetAddressForRandomNftSale(address) {
const url = 'https://api.nft-maker.io/xxxx/xxxx/1/10000000'
console.log("url: " + url)
return fecth(url, {method: 'get'})
.then(response => response.json())
}
Documentation
API end points & documentation: https://api.nft-maker.io/swagger/index.html
Image
enter image description here

I think your call of the api (the url) is not correct. It must be:
https://api.easy-nft-projects.io/GetAddressForRandomNftSale/<api_key>/<project_id>/<number_of_nfts>/<amount_in_lovelace>
So you are missing the GetAddressForRandomNftSale in your url.

Related

Display data from Twitter API using axios

I got stuck with a Vue project. I need to display 10 tweets in a component with the hashtag "Vue", so I am trying to pull data (username, tweet, date) from Twitter API. I read the twitter dev documentation, googled for hours, but it all suggested a php or python script for handling this request. Isn't it possible with axios? I got the following code, but the server always responds 400.
Here is my sample code (the keys are mocked):
import axios from "axios";
var api_url = "https://api.twitter.com/1.1/search/tweets.json?q=from%3Atwitterdev&result_type=mixed&count=2";
var AUTH_OBJ = {
oauth_consumer_key: "CONSUMER_KEY",
oauth_nonce: "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg",
oauth_signature: "tnnArxj06cWHq44gCs1OSKk%2FjLY%3D",
oauth_signature_method: "HMAC-SHA1",
oauth_timestamp: "1318622958",
oauth_token: "TOKEN_FROM TWITTER_DEV",
oauth_version: "1.0"
};
axios.defaults.headers.common["Authorization"] = AUTH_OBJ;
function getData() {
axios.get(api_url)
.then(function (response) {
console.log(response)
});
}
Could someone please help?

Using fetch() with API

I try to use fetch() to get the data from https://swapi.co/
With this code I get undefined but in the "Network" section in chrome I see the data I need. How I can access it?
fetch('https://swapi.co/api/people/1/')
.then(resp => resp.json)
.then(obj => console.log(obj));
Hello this will fetch the data returning it as json
fetch('https://swapi.co/api/people/1')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});
If you have right environment for calling to the fetch API there maybe 2 result
You will get the right result data
You will get an error
fetch(url) // Call the fetch function passing the url of the API as a parameter
.then(function() {
// Your code for handling the data you get from the API
})
.catch (function() {
// This is where you run code if the server returns any errors
});
Use a catch for seen what is going wrong with your request

Service Call in Dojo2

I trying to get the data from server and trying to render in UI but while making service call I am getting the below error.
Calling service API:
const json = request('http://localhost:8080/part/findall').then(response => response.json());
Error:
Task.js:243 Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1
at parse (<anonymous>)
at Task.js:243
at handler (ExtensiblePromise.js:137)
I am getting the data from server but while assign to json getting that above error. Can you please help me where I am doing mistake.
My response Json:
[{"id":485,"orderno":"00605164","type":"typeA","description":"description"},{"id":486,"orderno":"00605164","type":"typeB","description":"description"}]
This sounds like a serverside issue and not dojo related.
I reproduced your example with a sample json :
const json = request('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json());
and the data is fine.
Here is the complete example
import request from '#dojo/core/request';
import xhr from '#dojo/core/request/providers/xhr';
request.setDefaultProvider(xhr);
const json = request('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json())
.then(data => console.log(data));
Although the xhr is optional (it is the default, I guess) - this is fine in a browser and if you replace xhr by node it works fine in Node.js too.
How is your Server transferring the JSON - maybe as html ?
Also: What exactly is your dojo version?
More information for request than on https://dojo.io can be found in
https://github.com/dojo/core/tree/master/src/request
You got an array as response, try to use
response[0].json()

Axios POST request not working when passed object

I am trying to do a post request in a vue js app using axios to a local API and the response is returning empty data. The post request on API is working fine using Postman tool. Below is my code
var the_data = {
title: 'This is title',
description: 'this is description'
}
axios.post('/api/snippets/insert', the_data)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
On the API end, I am using a simple PHP script and printing whole $_POST request data using this code
var_dump($_POST);
But this is returning empty array.
I was running into this as well. Axios does not send the POST data in the form you're expecting. You need something like http://github.com/ljharb/qs and then use axios.post('/api/snippets/insert', Qs.stringify(the_data)). Please note this build on cdnjs uses Qs, not qs.
Alternatives to qs would be e.g. JSON.stringify() or jQuery's $.param().

Can't get fetch(url) working in React-Native

I am trying to retrieve the json response from api url using fetch (method : GET) in react-native.
constructor(props){
super(props);
this.state = {jsonData: {}};
}
componentWillMount() {
console.log("inside componentWillMount");
fetch('http://localhost:3000/listData')
.then((response) => {console.log('response: '); return response.json();})
.then((responseJson) => {console.log('responseData: '+JSON.stringify(responseJson)); return this.setState({jsonData: responseJson});})
.catch((err) => {console.log(err)});
}
The api returns data in this format:
{
object: 'list',
data: [...]
}
The api url works via curl. I also tried to run the fetch part of the code standalone using node by installing node-fetch and it printed the responseData properly.
But, in react-native, it doesn't print any of the console log statements inside the then function of fetch nor does it set the state of jsonData.
Could you please tell me what could be the problem? I have been googling around for quite a long time trying to find what could be the issue.
EDIT
I tried the async fetch as follows:
componentWillMount() {
console.log("inside componentWillMount");
this.fetchData().done();
}
async fetchData(){
const response = await fetch('http://localhost:3000/listData')
const json = await response.json();
const data = json.url;
console.log('data'+url);
}
Still, the same issue persists.
I am not able to understand why it works with the facebook url and not my local api url
SOLUTION
Thanks to Michael Cheng for pointing me in the right direction.
I found this link :
https://github.com/react-community/create-react-native-app/issues/154 .
Code Fix:
I just replaced the localhost with my ipv4 address as http://myserveripv4address:3000/listData and it worked.
Version:
react-native#0.50.4
Device : Android
some ideas:
if is iOS, you need the remote url to be https or disable this security feature;
You may need to mark your fetch() as async function;
Try to make an async fetch w/o promise.