twitchtv / twirp

A simple RPC framework with protobuf service definitions
https://twitchtv.github.io/twirp/docs/intro.html
Apache License 2.0
7.2k stars 326 forks source link

API question regarding empty requests #51

Closed c-s-alex closed 6 years ago

c-s-alex commented 6 years ago

Awesome framework!! 🥇 👍 👍 👍

Im new to RPC and i looked around all over and i didn't find what i was looking for so as a last resort im posting my issue here.. so please forgive me if it does not much sense.

Im having an issue with a simple .proto file i made for a hash generator API endpoint

syntax = "proto3";
package store.users.auth;

service Session {
    rpc Generate(Empty) returns (SessionResp);
  }

  message Empty {}

  message Session {
    string text = 1;
  }

This function generates a hash key that gets returned to the frontend and saved in a db that i use for another part of my app. How can i trigger the API without sending any JSON ... this works fine now, the only think i need to do is to send a empty json object {}

and i get my expected result

{
    "key": "5f6ce9c6-4276-40aa-86e0-326134ce8a9b"
}

Otherwise i get this

{
    "code": "internal",
    "msg": "failed to parse request json: EOF",
    "meta": {
        "cause": "*store_users_auth.wrappedError"
    }
}

How can i fix it in the .proto file so that the generate function's request ( Empty ) will work without an empty JSON post

Im hoping there is a easy way to do this because i don't to mess around with the .pb.go or the .twirp.go files.

Please help a newb :)

nawuko commented 6 years ago

An empty string is not valid json, therefore this is not supported by twrip atm. Here is a comment from the owner of twirp which could be interesting for you: https://github.com/twitchtv/twirp/issues/42#issuecomment-359348653.

Also this issue is a duplicate of https://github.com/twitchtv/twirp/issues/42 and https://github.com/twitchtv/twirp/issues/21