小编Jac*_*pia的帖子

如何使用 python 请求发送表单数据?

我正在尝试使用 python 3 和 requests 库发送 POST 请求。当我使用邮递员时,我得到了我期望的结果,因此我复制了邮递员生成的代码,这样它就可以工作了。

这是邮递员代码生成的代码:

import requests

payload = "name=\"claveElector\"\r\n\r\nABCDEF01234567H400\r\nname=\"numeroEmision\"\r\n\r\n01\r\nname=\"ocr\"\r\n\r\n4158093946570\r\nname=\"g-recaptcha-response\"\r\n\r\nsome-captcha\r\nname=\"modelo\"\r\n\r\na"

url = "https://listanominal.ine.mx/scpln/resultado.html"

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"claveElector\"\r\n\r\nTPRSJC95010209H400\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"numeroEmision\"\r\n\r\n01\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ocr\"\r\n\r\n4158093946570\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"g-recaptcha-response\"\r\n\r\nsome-re-captcha\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"modelo\"\r\n\r\na\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
}

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)
Run Code Online (Sandbox Code Playgroud)

更清楚的是为什么不起作用的部分是两个结果中存在的差异。

通过邮递员代码我得到了这个

该代码是使用以下数据生成的: 该代码是使用以下数据生成的

所以我尝试使用自己的代码执行相同的操作,我尝试发送文件部分和数据部分中的数据,但不起作用。阅读其他 StackOverflow 问题后,他们建议使用属于 Requests Toolbelt 库一部分的 MultipartEncoder。

所以我的实现最终是这样的:

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

clave_elector = "ABCDEF01234567H400"
numero_emision = "01"
ocr = "1234567846570"
modelo = "a"

params = {
    "claveElector": clave_elector,
    "numeroEmision": numero_emision, …
Run Code Online (Sandbox Code Playgroud)

python multipartform-data python-3.x python-requests postman

7
推荐指数
1
解决办法
3万
查看次数

Vue,$是什么意思?

我正在学习Vue.js,但我不了解这些$符号的作用。我正在使用Laravel,我的意思是我没有在使用Vue-CLI。当我转到Vue文档时,很多文档都没有$

例如,“程序化导航”部分说:router.push({ path: '/posts' }),但是当我在代码中这样做时,我必须做this.$router.push({ path: '/posts' });

提前致谢。

laravel vue.js vuejs2

4
推荐指数
3
解决办法
562
查看次数

mixin 和泛型有什么区别?

我正在学习 Django Rest Framework。并且有两个概念在我看来几乎是一样的,并且用于不同的场景。

rest_framework mixins我认为当我们使用视图集时会使用它们。并rest_framework generics与 APIViews 一起使用。

这两个组件有什么区别?

django mixins django-generic-views django-rest-framework

3
推荐指数
1
解决办法
2917
查看次数

Laravel 生成器给出:无法重新声明 generatorFunction()

我有以下代码,但是当我运行我的工厂时,出现以下异常:

无法在 /Users/user/Desktop/my-app/database/factories/QuestionFactory.php 中重新声明 questionIndex()(之前在 /Users/user/Desktop/my-app/database/factories/QuestionFactory.php:42 中声明)第 46 行

当我运行我的单元测试时会发生这种情况,而这个特定的工厂现在不在测试中。我有其他工厂有一个生成器,但函数的名称完全不同。被称为autoIncrement()

<?php

use Faker\Generator as Faker;

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/

$questionIndex = questionIndex();

$factory->define(App\Models\Question::class, function (Faker $faker, $overrides) use ($questionIndex) {

    $question = [
        'How is 2 + 2?',
        'Choose, what …
Run Code Online (Sandbox Code Playgroud)

php generator laravel

2
推荐指数
1
解决办法
544
查看次数

React Native:FlatList 索引表示未定义

我正在尝试在 React Native 中检索 FlatList 上单击元素的索引。正如文档所说,我将索引传递给 renderItem 道具。我的代码如下:

/**
 * Goes to the show view of a container
 * @param {*} index 
 */
showContainer = (index) => {
    console.log(index); 
}

render() {
    return (
        <DefaultScrollView style={styles.container}>
            <FlatList
                data={this.props.containers}
                renderItem={(data, index) => (
                    <ListItem
                        containerStyle={{borderBottomWidth: 1, borderBottomColor: "#000"}}
                        key={data.item.id}
                        leftAvatar={Image}
                        onPress={() => {this.showContainer(index)}}
                        rightIcon={{ name: "ios-eye", type: "ionicon" }}
                        subtitle={
                            `${data.item.dummy === true? 'Por favor configura tu dispositivo' : 'Contenido actual: '}`
                        }
                        subtitleStyle={(data.item.dummy == true)? [styles.configurationText, styles.subtitule] : styles.subtitule}
                        title={data.item.name} …
Run Code Online (Sandbox Code Playgroud)

reactjs react-native react-native-flatlist react-native-elements

2
推荐指数
1
解决办法
2560
查看次数

Laravel,如何验证枚举列

我正在尝试在 Laravel 中进行枚举列验证。这是我的验证器的代码。

\n\n
/**\n * Returns the rules and messages for validating this creation\n */\npublic static function ValidationBook($except = [], $append = []) {\n    $book = [\'rules\' => [], \'messages\' => []];\n    $arr = config(\'constants.publication_statuses\');\n    $arrKeys = array_keys($arr);\n    $book[\'rules\'] = [\n        \'concert.title\' => \'required|string\',\n        \'concert.user_id\' => \'required|exists:users,id\',\n        \'concert.type\' => [\n            \'required\',\n            Rule::in([\'public\', \'private\']),\n        ],\n        \'concert.status\' => \'required\',\n        \'concert.closes_on\' => \'nullable\'\n    ];\n    $book[\'messages\'] = [\n\n        \'concert.title.required\' => \'El t\xc3\xadtulo es requerido.\',\n        \'concert.title.string\' => \'El t\xc3\xadtulo debe ser un texto\',\n\n        \'concert.user_id.exists\' …
Run Code Online (Sandbox Code Playgroud)

laravel eloquent laravel-validation

1
推荐指数
1
解决办法
4318
查看次数

Laravel 抛出自定义 ValidationException

我正在尝试抛出自定义异常。该项目是一个使用 Google Places API 的 API,如果结果状态为ZERO RESULTS我需要抛出验证异常。这是因为存储的数据是错误的。

为了更清楚这一点。注册用户修改他的个人资料,包括地址、邮政编码等。然后我们有一个 get 方法,我们在其中查询地点 API,这是为了获取纬度和经度并将其添加到个人资料模型中。

if (strcmp($status, 'ZERO_RESULTS') == 0 || strcmp($status, 'INVALID_REQUEST') == 0) {
    $error = ValidationException::withMessages([
        "one_thing" => ["Validation Message #1"], "another_thing" => ['Validation Message #2']
    ]);
    throw $error;
}
Run Code Online (Sandbox Code Playgroud)

我在 StackOverflow 上阅读了一些答案,我什至正在尝试这些答案,但我只收到以下错误:

{
  "errors": [
    {
      "status": 500,
      "code": 1,
      "source": {
        "pointer": "ErrorException line 73 in /Users/jacobotapia/Documents/Espora/one-mind-backend/vendor/sfelix-martins/json-exception-handler/src/ValidationHandler.php"
      },
      "title": "errorexception",
      "detail": "Undefined index: one_thing"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

另外我想指出所有过程都发生在一个GET方法中。

我唯一想要的是返回一个错误,指出我们无法从谷歌地点 API 获得任何结果。这是为了向客户说明用户在应用程序中注册的个人资料数据是错误的。

我究竟做错了什么?

php laravel laravel-5

1
推荐指数
1
解决办法
5126
查看次数

Laravel:缺少必需的参数

我通过missing required parameters以下路线收到错误: Route::get('/myunits/{group}/content/{unit}','Users\GroupContentController@show')->name('user.group.unit.show');

当我们重定向到该路由时,该路由是正确的,但是由于某种原因它失败了。但是,当我在参数上执行dd()时,参数就GroupContentController@show在那里,所以我不知道错误在哪里。

这是我的控制器

public function show(Group $group , Unit $unit) {


    /*
    * Check if the user is trying to acces a group
    * here he does not belongs.
    */
    if ( !UserGroupFacade::IsStudentInGroup($group) ) {
        return redirect()->route('user.groups');
    }

    $data = [];
    $groupMaterials = $group->groupMaterials->where("unit_id" , $unit->id);

    foreach ($groupMaterials as $gm) {
        foreach ($unit->themes as $theme) {
            if ($theme->id == $gm->theme_id) {
                $theme->show=true;
                $material=$theme->materials->where("id" , $gm->material_id)->first();
                $material->show=true;
            }
        }
    }

    $data['unit'] = $unit;
    return …
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-5

0
推荐指数
1
解决办法
202
查看次数