In flutter dart am making an API call like below
Future<dynamic> fetchInstitutionsCall() {
List<int> requestedItemsList =[18,5];
return ApiManager.instance.makeApiCall(
callName: 'fetchInstitutions',
apiUrl: 'https://localhost:3000/api/v1/find_institutions/',
callType: ApiCallType.GET,
params: {
'user': 'sam',
'requested_items': requestedItemsList,
},
returnResponse: true,
);
}
The above request when received in server is received as a string instead of an array for the requested_items params like below
Parameters: {"user"=>"sam", "requested_items"=>"[18,5]"}
But the server is expecting in below format
Parameters: {"user"=>"sam", "requested_items"=>["18", "5"]}
How can we send a request in flutter so that requested_items params array won't get stringified when received in server?
Thank you
Related
I want to check if my Linkedin Pixel tag is installed properly inside my webpages.
To validate, I would need to get a 302 response when the tag is fired.
Trying to create a chrome extension that could perform this validation.
chrome.webRequest.onSendHeaders.addListener(
(e) => {
console.log(e);
},
{
urls: ["https://px.ads.linkedin.com/*"],
}
);
--console.log object--
{documentId: 'E3A1D48B4697AC34E10A4D2A888CC8A9', documentLifecycle: 'active', frameId: 0, frameType: 'outermost_frame', initiator: 'https://www.theb2bhouse.com', …}
documentId: "E3A1D48B4697AC34E10A4D2A888CC8A9"
documentLifecycle: "active"
frameId: 0
frameType: "outermost_frame"
initiator: "https://www.theb2bhouse.com"
method: "GET"
parentFrameId: -1
requestId: "2395"
tabId: 2
timeStamp: 1652447005855.711
type: "image"
url: "https://px.ads.linkedin.com/collect?v=2&fmt=js&pid=2742940&time=1652447005855&url=https%3A%2F%2Fwww.theb2bhouse.com%2F"
[[Prototype]]: Object
Does anyone know how to send a webRequest to check if this above details got back a 302-redirect response?
On my inspect -> network request, I could see that the tag is fired correctly and recieved a response code of 302. Yet, I find the correct method to do the validation on webRequest.
I am implementing Stripe payments, but am unable to submit metadata as a query param as per the documentation, it seems as though it wants iterable stringified key-value pairs which I have tried to achieve with the below with no luck.
Question: How can I pass metadata to Stripe using Dart and the HTTP library?
class StripeServices {
static var client = http.Client();
static var stripeTestKey =
'privatesecretkeyfromstripe';
static Future<void> createStripeCustomer() async {
Map<String, String> metadata = {'uuid': '123456'};
Uri uri = Uri(
scheme: 'https',
host: 'api.stripe.com',
path: '/v1/customers',
queryParameters: {
'description': 'Test Customer',
'metadata': metadata.entries.toList().toString()
});
var response = await client.post(
uri,
headers: {
'Authorization': 'Bearer ' + stripeTestKey,
},
);
print(response.body);
}
The error I am getting back from the endpoint is
> flutter: { "error": {
> "message": "Invalid object",
> "param": "metadata",
> "type": "invalid_request_error" } }
calling Stripe API requires a secret API key, and you shouldn't store and use the API key in your frontend application because it's not safe.
Instead you should make the API call from your backend where the API key can be securely stored.
I need to send some data in request body of a get request in Ionic Http native.
I am using the below code to send the get request, but this converts the data to query string.
this.http.get(
'apiendpoint',
data ,
headers
);
}
using the below plugin :
import { HTTP } from '#ionic-native/http/ngx';
below is the link to official documentaation:
https://github.com/silkimen/cordova-plugin-advanced-http
if you want to send data, you should use POST method, instead of GET method:
import { HTTP } from '#ionic-native/http/ngx';
constructor(private http: HTTP) {}
...
this.http.post(
'apiendpoint',
data,
headers
);
}.then(data => {
console.log(data.status);
console.log(data.data); // data received by server
console.log(data.headers);
})
.catch(error => {
console.log(error.status);
console.log(error.error); // error message as string
console.log(error.headers);
});
I have an app built with Nuxt.js. To get data from the API I use axios, namely #nuxtjs/axios. All request work fine but for the DELETE method.
My syntax is the following:
async removeItemFromCart(productId) {
const accessKey = await this.$store.dispatch('fetchUserAccessKey');
try {
this.$axios.delete(`/api/baskets/products?userAccessKey=${ accessKey }`, {
data: {
productId: productId
}
})
} catch (error) {
console.log(error);
}
},
However, in the console I always get the following error: createError.js?2d83:16 Uncaught (in promise) Error: Request failed with status code 400
I tried to use params instead of data, but to no avail. What am I missing here?
It seems that there's an issue with axios: when you use delete method with body, it either doesn't include payload, or deletes Content-type: 'application/json' from headers. To solve the issue, I used .request instead of .delete (according to https://github.com/nuxt-community/axios-module/issues/419)
this.$axios.request(`/api/baskets/products?userAccessKey=${ accessKey }`, {
data: {
productId: productId
},
method: 'delete'
}).
let name = "myname"
function updateTodo(name) {
axios.put('https://jsonplaceholder.typicode.com/todos/1', {
title: name,
completed:true
})
.then(res=>showOutput(res))
.catch(err=>console.log(err))
}
I just wanted to send name variable as a function parameter to title data and it shows me that am sending empty data on how to fix this one.
If you want to send JSON in an HTTP request body, remember to set the header Content-Type to application/json.
E.g:
const body = {
title: name,
completed: true
};
axios.put('https://jsonplaceholder.typicode.com/todos/1', body, {
headers: {
'Content-Type': 'application/json'
}
});
Hope it helps.
Enjoy!