Giter Site home page Giter Site logo

Comments (5)

timostamm avatar timostamm commented on June 6, 2024

You cannot include credentials with Access-Control-Allow-Origin: *. You have to allow specific origins instead.

This is unrelated to protobuf-ts - it's how browsers handle cross-origin requests. You will not be able to work around this with simple requests or no-cors mode.

from protobuf-ts.

hengyunabc avatar hengyunabc commented on June 6, 2024

@timostamm Thank you for your reply.

Sorry, I made a mistake above. Setting Access-Control-Allow-Headers to * is not allowed.

Currently I set Access-Control-Allow-Origin to aaa.test.com.

I tested that if use the fetch api, you can specify headers and there will be no extra headers. At the same time, set Access-Control-Request-Headers: client-id on the server side, so that the server can handle the request correctly.

fetch("https://aaa.test.com/servic/method", {
   "headers": {
     "accept": "application/grpc-web-text",
     "client-id": "xxxx",
   }
   "

Is there any way to control headers when using protobuf-ts?

from protobuf-ts.

timostamm avatar timostamm commented on June 6, 2024

At the same time, set Access-Control-Request-Headers: client-id on the server side, so that the server can handle the request correctly.

That's incorrect. Access-Control-Request-Headers is a header sent by the browser automatically for preflight requests.

You'll have to spend some time to read up on the documentation for CORS, run some examples yourself, and set up CORS correctly on your server. I highly recommend to use a CORS middleware if available.

The gRPC-web transport just uses fetch. It adds a couple of protocol-specific headers that you should not remove, otherwise your gRPC-web requests are broken. You can add your own headers via RPC options, see the manual.

from protobuf-ts.

jcready avatar jcready commented on June 6, 2024

@hengyunabc the grpc-web transport must set the Content-Type request header to application/grpc-web-text or application/grpc-web+proto. This is probably the bare minimum headers your server will need to respond with to work with CORS grpc-web requests:

Access-Control-Allow-Methods: OPTIONS, POST
Access-Control-Allow-Headers: accept, content-type, grpc-timeout, x-grpc-web
Access-Control-Expose-Headers: grpc-message, grpc-status

from protobuf-ts.

hengyunabc avatar hengyunabc commented on June 6, 2024

Thanks for replies, I made a stupid mistake.

Access-Control-Request-Headers are sent by the browser to the server.

Access-Control-Allow-Headers is sent by the server to the browser.

The browser will send the following request header to the server:

Access-Control-Request-Headers: bx-v,cache-control,client-id,content-type,pragma,x-grpc-web

So server needs to return:

Access-Control-Allow-Headers: bx-v,cache-control,client-id,content-type,pragma,x-grpc-web

If Access-Control-Allow-Headers: * is returned, or Access-Control-Allow-Headers: client-id is returned, it will fail.

from protobuf-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.