问题描述
我有如下要求
val request =
Request[IO](
method = POST,uri = Uri.uri("..."),headers = Headers(
Authorization(BasicCredentials("...","..."))
)
)
.withEntity(PaymentIntentRequest2(2000,"usd"))
我正在寻找at the source code,看起来withEntity
继承了嵌套EntityDecoder
的标头,因此上面的代码默认为Content-Type: application/json
。好像我明确传递了UrlForm
一样,一切都很好。
不幸的是,我遇到的API期望数据为x-www-form-urlencoded
,并且鉴于目标API具有所有不同端点/请求的复杂性,我想找到一种将给定case类编码为表单的方法。最好的方法是什么?
我尝试过:
-
明确指定
Content-Type
,但此操作无效,因为继承的类型具有优先级 -
构建从
Product
到UrlForm
(目前的扩展方法)的隐式通用转换
implicit class UrlFormEncode[+B <: Product](val u: B) {
def asUrlForm: UrlForm =
u.productElementNames
.zip(u.productIterator)
.foldLeft(UrlForm()) { (a,b) =>
a.combine(UrlForm(b._1 -> b._2.toString))
}
}
这里的问题是UrlForm
在映射的两面都需要一个字符串。如果我仅用.toString
进行转换,则由于例如嵌套类型而无法正常工作
ChargeRequest(Amount(refInt),EUR,source = Some(SourceId("...."))
导致以下json
无效
{
"currency": "EUR","amount": "2000","source": "Some(SourceId(....))","customer": "None"
}
我尝试使用asJson
而不是toString
,但是大约无法确定适当的KeyEncoder
解决此问题的正确方法是什么,以便将给定的Product
向下编码?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)