Back

POST API request via function

  • 0
  • Functions
katskoru
19 Mar, 2023, 21:40

Hello, In the cloud function, I try to make a POST API request, after which I need an appropriate response. And here is a strange situation - when I send a same request to the same API via POSTMAN or via a function from my local mobile application, I receive a correct response, while when the request is sent via cloud function, I receive the response status code 403 Forbidden. I was blocked by CORS policy when trying to send a request from a web application. Could this be the same type of problem? Could the problem is somewhere else?

TL;DR
The user is experiencing a 403 Forbidden error when sending a POST API request from a cloud function. They suspect it may be a CORS issue. Other users suggest checking the headers, comparing them to the headers used in Postman, and ensuring they are set correctly. One user suggests returning the headers in the JSON response for comparison. Another user provides an alternative code snippet for sending the request. No solution is provided in the thread.
Binyamin
19 Mar, 2023, 21:42

What is the context? How do you run the request with what function on which environment?

katskoru
19 Mar, 2023, 22:12

I execute a cloud function, where the API request is placed.

Binyamin
19 Mar, 2023, 22:13

Okay. So fetch inside node js deployed function ?

katskoru
19 Mar, 2023, 22:27

The function is properly deployed and execution status is completed. The request made to another API works fine, but it is clearly visible that in this case (API with CORS policy) there is a problem in Appwrite cloud function.

Binyamin
19 Mar, 2023, 22:29

I understand But if you are using the node SDK that it's pretty not possible for it to meet CORS condition as the request is from a CLI and not from HTTP Can you share your code

katskoru
19 Mar, 2023, 22:45

I am using Dart SDK. Here is my code:

katskoru
19 Mar, 2023, 22:45
TypeScript
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:dart_appwrite/dart_appwrite.dart';

Future<void> start(final req, final res) async {

var headers = {
  'Authorization': apiKey,
  'Content-Type': 'application/json'
};
var request = http.Request('POST', Uri.parse('https://api.myapi.com/v1/products'));
request.body = json.encode({
  "type": "Product",
  "name": "Gloves",
  "condition": "New",
  "code": "714",
  "imei": "714",
  "category": "",
  "model": "655954",
  "additionalModels": "",
  "status": "Active",
  "oneTimeUse": false,
  "inventoryControl": true,
  "TaxIncludedInCost": true,
  "cost": 0,
  "TaxIncludedInPrice": true,
  "price": 0,
  "enableOfferPrice": true,
  "offerStart": "15-Feb-2023 13:10",
  "offerEnd": "06-Mar-2024 13:44",
  "TaxIncludedInOfferPrice": true,
  "offerPrice": 5,
  "tax": 34513,
  "discountable": true,
  "description": "desc",
  "stockQuantity": 8
});
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200 || response.statusCode == 201) {
 res.json({'res': await response.stream.bytesToString()});

  print(await response.stream.bytesToString());
}
else {
 res.json({'res': response.reasonPhrase});

}

}
Binyamin
19 Mar, 2023, 22:53

So over here you get CORS?

katskoru
19 Mar, 2023, 22:53

over here I got 403 Forbidden

Binyamin
19 Mar, 2023, 22:57

Ohh, So here you don't get CORS,

I would recommend you'll check the headers, that the

TypeScript
var headers = {
  'Authorization': apiKey,
  'Content-Type': 'application/json'
};

That they are exactly like in the postman For start return the header in the json

TypeScript
 res.json({
'res': response.reasonPhrase
'headers': headers
});

And compere them to the Postman one

Because so far you code looks intact

Binyamin
19 Mar, 2023, 22:58

And I assume that you've redact the apiKey for the example, right?

katskoru
19 Mar, 2023, 23:09

Headers is the same. I copied the generated code snipped from POSTMAN to my cloud function. And that's right - apiKey is for example

Binyamin
19 Mar, 2023, 23:16

It's really weird as CORS cannot occurs when running CLI, and the only problem is the 403 So, what I think can be the problem is that somehow your wanted API are blocking you server IP from any reason.
And just to prove it try this way instead of the one you're using (Which is how I send my requests)

TypeScript
import 'package:dart_appwrite/models.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:dart_appwrite/dart_appwrite.dart';

Future<void> start(final req, final res) async {
  var headers = {
    'Authorization': apiKey,
    'Content-Type': 'application/json'
  };

  final data = {
    "type": "Product",
    "name": "Gloves",
    "condition": "New",
    "code": "714",
    "imei": "714",
    "category": "",
    "model": "655954",
    "additionalModels": "",
    "status": "Active",
    "oneTimeUse": false,
    "inventoryControl": true,
    "TaxIncludedInCost": true,
    "cost": 0,
    "TaxIncludedInPrice": true,
    "price": 0,
    "enableOfferPrice": true,
    "offerStart": "15-Feb-2023 13:10",
    "offerEnd": "06-Mar-2024 13:44",
    "TaxIncludedInOfferPrice": true,
    "offerPrice": 5,
    "tax": 34513,
    "discountable": true,
    "description": "desc",
    "stockQuantity": 8
  };

  try {
    var response = await http.post(Uri.parse('https://api.myapi.com/v1/products'),
      headers: headers,
      body: jsonEncode(data),
    );


    if (response.statusCode == 200 || response.statusCode == 201) {
      print(await response.body);

      // res.json({'res': await response.body});
    }
  } catch (e) {
    res.json({'res': response.reasonPhrase});
  }
}
joeyouss
24 Mar, 2023, 17:56

Hi @katskoru is your query resolved?

katskoru
25 Mar, 2023, 20:39

Hello @joeyouss Unfortunately not.

Drake
25 Mar, 2023, 22:48

How are you initializing the apiKey? You might want to return the response body from your API.

Drake
25 Mar, 2023, 22:52

Btw, there's no need to await response.body

Binyamin
26 Mar, 2023, 00:28

Agreed it was copied from the original one

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more